Archived

This topic is now archived and is closed to further replies.

Virtual functions and linked list destructors... HELP, I'M GOING CRAZY!!

This topic is 5777 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, this about to drive me right up the wall trying to find a solution, but here it goes: The Problem: I have a cyclic multiple-level linked list and everything's fine until I got to delete it. Everything is derrived from public CObject which is derrived from the base class of CNode. So when I delete the head of an inherited class, ~CNode is called. CNode then does this:
~CNode()
	{
		Detach();
				
		while (pChild)
		{
			sprintf(buffer, "Report from %d: Deleting Child %d", id, pChild->id);
			MessageBox(NULL, buffer, "Info", MB_OK | MB_ICONINFORMATION);
			delete pChild;
		}

		sprintf(buffer, "Report from %d: Checkpoint", id);
		MessageBox(NULL, buffer, "Info", MB_OK | MB_ICONINFORMATION);
	}
   
So, say I have a class derrived from CObject with another class also derrived from CObject attached to it. When the program goes to delete the head of the linked list it detaches it, goes to the child deconstructor, detaches the child, deletes the child, but doesn't return to delete itself causing a debug assertion failure!! The reason (for God knows why) is because CObject has a virtual function inside of it that it is essential; however, it is the virtual function in CObject that causes the problem. The function is declared like: virtual void Main(void) { } However, for some reason it screws up the CNode deconstruction process so that the parent never deletes itself. WHY? And how can I solve this problem before I go absolutely insane and pull all of my hair out? PLEASE PLEASE PLEASE HELP! PLEASE! Thankyou, -Jesse Edited by - Aphelion on February 20, 2002 10:13:17 PM Edited by - Aphelion on February 20, 2002 10:13:30 PM

Share this post


Link to post
Share on other sites
Does calling Detach() go back to the parent and tell it to set pChild to NULL? If not, you''ll free a child twice. I''m a bit confused at what you mean when you say, "but doesn''t return to delete itself causing a debug assertion failure" . You don''t get assertion failures by not doing something - you get them from doing something wrong - often a double free. Knowing exactly what Detach does might help here.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites
Yes, the Detach() process does set the parent to null. So when detach is called on the child, pChild is thus set to null. Judging by the message boxes I set up for debugging, this is what happens from what I can best figure:
delete parent:
parent->Detach();
parent has child, so delete child
child->Detach(); parent->pChild = NULL;
child checks in just before deleting at the "checkpoint"
Instant debug assertion failure.
From what I can figure, the debug assertion failure is caused because the parent never calls back that it is at the "checkpoint" just before being deleted. So the delete parent function is never finished, but interrupted at the very end by the Debug Assertion Failure. However, if I remove the virtual function from CObject (derived from CNode), the class that both the parent and child are derived from, the code works exactly as expected, with the parent finishing its loop and calling in at the "checkpoint". WHY?!??
I really need some help, this step is, needless to say, crucial to my engine! Please help!
Thank you,
-Jesse

Share this post


Link to post
Share on other sites
Ah, finally, I found the solution. Apparently, the CNode deconstructor has to be a virtual function for it to work correctly... I''ve heard of that before, but I''d just forgotten... Well, at least the problem is fixed. Now I can get back to the fun, fun scene management! Oh joy! (sarcasm)
Thanks,
-Jesse

Share this post


Link to post
Share on other sites