Click here to Skip to main content
15,903,201 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionAdding member variables crashes app Pin
Wheatbread31-Jul-06 9:49
Wheatbread31-Jul-06 9:49 
AnswerRe: Adding member variables crashes app Pin
Chris Losinger31-Jul-06 9:57
professionalChris Losinger31-Jul-06 9:57 
GeneralRe: Adding member variables crashes app Pin
Wheatbread31-Jul-06 10:34
Wheatbread31-Jul-06 10:34 
AnswerRe: Adding member variables crashes app Pin
Hans Dietrich31-Jul-06 10:02
mentorHans Dietrich31-Jul-06 10:02 
GeneralRe: Adding member variables crashes app Pin
Wheatbread31-Jul-06 10:35
Wheatbread31-Jul-06 10:35 
AnswerRe: Adding member variables crashes app Pin
Zac Howland31-Jul-06 10:13
Zac Howland31-Jul-06 10:13 
GeneralRe: Adding member variables crashes app Pin
Wheatbread31-Jul-06 10:37
Wheatbread31-Jul-06 10:37 
GeneralRe: Adding member variables crashes app [modified] Pin
Zac Howland31-Jul-06 10:47
Zac Howland31-Jul-06 10:47 
Yes. The destructor MUST be declared virtual if you plan to derive from it. Here is why:

class Base
{
public:
	Base() {}
	~Base() {}
private:
	// whatever you want here
};

class Derived : public Base
{
public:
	Derived() {}
	~Derived() {}
private:
	int m_MyInt;
};

void main()
{
	Base* pB = new Derived;
	delete pB;	// memory leak
}


What happens is that the ~Base() destructor is called, but not the ~Derived(). Thus, m_MyInt stays allocated in memory (that is, the portion of memory allocated for a Base object is freed, but not that for Derived). This is called memory slicing (see "Effective C++" by Scott Meyers for more details).

The rule of thumb: If your class is meant to have classes derived from it, it MUST have a virtual destructor.

This problem can (and will) blow up your memory eventually (either stack or heap or both).


-- modified at 16:48 Monday 31st July, 2006

If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week

Zac

GeneralRe: Adding member variables crashes app Pin
Wheatbread31-Jul-06 11:06
Wheatbread31-Jul-06 11:06 
GeneralRe: Adding member variables crashes app Pin
Zac Howland31-Jul-06 11:14
Zac Howland31-Jul-06 11:14 
GeneralRe: Adding member variables crashes app Pin
Wheatbread1-Aug-06 12:28
Wheatbread1-Aug-06 12:28 
GeneralRe: Adding member variables crashes app Pin
Zac Howland1-Aug-06 17:07
Zac Howland1-Aug-06 17:07 
Questionsubitem in CListCtrl Pin
Tara1431-Jul-06 9:46
Tara1431-Jul-06 9:46 
AnswerRe: subitem in CListCtrl Pin
Hans Dietrich31-Jul-06 10:07
mentorHans Dietrich31-Jul-06 10:07 
GeneralRe: subitem in CListCtrl Pin
Tara1431-Jul-06 18:37
Tara1431-Jul-06 18:37 
QuestionRe: subitem in CListCtrl Pin
David Crow31-Jul-06 10:20
David Crow31-Jul-06 10:20 
AnswerRe: subitem in CListCtrl Pin
Tara1431-Jul-06 18:53
Tara1431-Jul-06 18:53 
AnswerRe: subitem in CListCtrl Pin
Michael Dunn31-Jul-06 17:57
sitebuilderMichael Dunn31-Jul-06 17:57 
GeneralRe: subitem in CListCtrl [modified] Pin
Tara1431-Jul-06 19:39
Tara1431-Jul-06 19:39 
AnswerRe: subitem in CListCtrl Pin
Hamid_RT31-Jul-06 18:52
Hamid_RT31-Jul-06 18:52 
GeneralRe: subitem in CListCtrl Pin
Tara1431-Jul-06 18:58
Tara1431-Jul-06 18:58 
GeneralRe: subitem in CListCtrl Pin
Tara1431-Jul-06 21:53
Tara1431-Jul-06 21:53 
Questiondebugging VS6.0 C (not ++)... how to view entire array contents? Pin
Jesse Evans31-Jul-06 9:35
Jesse Evans31-Jul-06 9:35 
AnswerRe: debugging VS6.0 C (not ++)... how to view entire array contents? Pin
David Crow31-Jul-06 9:47
David Crow31-Jul-06 9:47 
GeneralRe: debugging VS6.0 C (not ++)... how to view entire array contents? Pin
Jesse Evans31-Jul-06 11:15
Jesse Evans31-Jul-06 11:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.