C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-04-2004, 12:09 PM   #1
Registered User
 
Join Date: Oct 2003
Posts: 97
Problem with destructors.

I'm working on a slightly complex application and I have a problem. A few destructors are not getting called when the program quits.
I have a few classes, all are initialized just fine, the program runs fine, no crashes or anything. The problem is that when I quit I find a lot of memory leaked. I checked everything and I was able to fix some of them, the problem was that I was not freeing those variables. But I have a problem where memory is leaked at exit even though I free the momory in the destructor. Basically its
OBJECT_MANAGER which manages all the Objects, then here are loaded the Objects from a file, when the text is parsed a new OBJECT is created. In the OBJECT class there are different classes depending on the parsed text. If its is a sound then SOUND_MANAGER is called and depending on the sound format it may call the MP3_CLASS or OGG_CLASS. The problem is that the OBJECT destructor is not called at exit so nothing is freed, I end up with a lot of memory leaked. How can I make sure that the destructor of each class gets called?

THANKS!
Hulag is offline   Reply With Quote
Old 06-04-2004, 12:23 PM   #2
Registered User
 
Join Date: May 2003
Posts: 82
Quote:
Originally Posted by Hulag
A few destructors are not getting called when the program quits.
I don't have much experience, but that seems suspect. Assuming the code compiles, a destructor is called everytime an object goes out of scope. The source of your problem would be that the wrong destructor gets called (ie a default constructor), or problem within the way the destructor deallocates memory.

I'd suggest posting the code, as most likely memory is allocated somewhere within one of the classes, but ignored by the appropriate destructor.
AH_Tze is offline   Reply With Quote
Old 06-04-2004, 12:24 PM   #3
Registered User
 
Join Date: Aug 2001
Posts: 223
can you post some of your code where you are creating the objects. This may help clarify the situation.
__________________
zMan
zMan is offline   Reply With Quote
Old 06-04-2004, 02:43 PM   #4
Registered User
 
Join Date: Apr 2002
Posts: 1,571
As stated before, it's hard to tell when you don't have source to look at. However, if you are using inheritance then you will want to look into using virtual destructors. Declare your base class destructors as virtual so that the derived class destructors will be called properly.
__________________
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers
MrWizard is offline   Reply With Quote
Old 06-04-2004, 06:49 PM   #5
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 5,249
>> The problem is that the OBJECT destructor is not called at exit so nothing is freed, I end up with a lot of memory leaked.

If you used 'new' to allocate it, don't expect the destructor to be called when the program exits - it won't. Instead, just create a global instance (outside of main) as a stack variable. At that point, if the destructor isn't called then there is something very wrong with the compiler you are using.
__________________
Code:
int main(void){srand(time(0));for(double l=rand(),l0=0,l00=0;;l0+=0.1){for(double l000=0;l000
<1;l000+=.001,l+=((double)rand()/RAND_MAX)/0x64,l00+=((sin(l*0x8*atan(l0)*l000-(l0*0x8*atan
(l)))*0.5)+0.5)){for(size_t l0000=0,l00000=(size_t)(0x50*(l00-floor(l00)));l0000<l00000;++l0000
)putchar(0x20);putchar(0x61+(int)((double)rand()/RAND_MAX*0x1a));putchar('\n');}}return 0;}
Sebastiani is offline   Reply With Quote
Old 06-05-2004, 12:59 AM   #6
Registered User
 
Join Date: Oct 2003
Posts: 97
Unfortunaly the code is just too much to just paste, and I can't release the whole code either. But I will try to explain my problem.

First, I start the OBJECT_MANAGER
Code:
OBJECT_MANAGER::StaticInstance()->Initiate(); // The StaticInstance is to avoid making copies, only one OBJECT_MANAGER in the whole program.
The Initiate() function loads the objects from a file and puts them in a temporary structure which basically holds a property name and value for every object. Then then from that info, the OBJECTs are created.

Code:
for (i = 0; i < num_objects; ++i)
{
	OBJECT * newObject=new OBJECT;
	if(!newObject)
	{
		LOGGING::StaticInstance()->Error("FAILURE!");
		return 0;
	}
	newObject->origin = object_vec3(i, "origin"); // Position of the object.
	newObject->angle = object_float(i, "angle"); // Angle faced by the object.
	const char *objectclass_val = object_value(i, "classname"); // Determine what type of object it is.
	if (!stricmp(objectclass_val, "sound")) // if it is a sound file
	{
		newObject->filename = object_value(i, "filename");
		if (strcmp(newObject->filename, "") != 0)
		{
			newObject->SoundManager = new SOUND_MANAGER; 
			newObject->SoundManager->SoundData = SOUND_MANAGER::StaticInstance()->LoadFile(newObject->filename);
		}
	}
	// Then other object_classes are processed.
}
Ok, now on the LoadFile
Code:
SOUND * newSound;

int filenameLength=strlen(filename);
if( 	strncmp(filename+filenameLength-3, "MP3", 3)==0 || strncmp(filename+filenameLength-3, "mp3", 3)==0)
{
	newSound=new MP3_SOUND;
}
else if(strncmp(filename+filenameLength-3, "OGG", 3)==0 || strncmp(filename+filenameLength-3, "ogg", 3)==0)
{
	newSound=new OGG_SOUND;
}
else
{
	LOGGING::StaticInstance()->Error("FAILURE!");
	return -1;
}
newSound->Load();

sounds.push_back(newSound);

return sounds.size()-1;
Now, I have destructors for all my classes. For example the constructor and destructor for OBJECT look something like this
Code:
OBJECT() : SoundManager(NULL)
{}

~OBJECT()
{
	if(SoundManager)
		delete SoundManager;
	SoundManager;
}
The problem that it doesn't even get called, which means that none of the other classes inside SoundManager will get called either.
How can I fix that? THANKS!

PS: Don't worry for errors or if it compiles or not, I just made a quick example to explain, its not the actual code.
Hulag is offline   Reply With Quote
Old 06-05-2004, 02:36 PM   #7
carry on
 
JaWiB's Avatar
 
Join Date: Feb 2003
Location: Seattle, WA
Posts: 1,971
Code:
OBJECT * newObject=new OBJECT;
Do you ever call delete on that? As Sebastiani said, the destructor won't automatically be called when it goes out of scope.
__________________
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)

"Work hard and it might happen."
-XSquared
JaWiB is offline   Reply With Quote
Old 06-11-2004, 12:30 PM   #8
Registered User
 
Join Date: Oct 2003
Posts: 97
I completly forgot to answer the thread, sorry about that. The problem was actually both cases, sometimes I didn't delete the new objects, and sometimes I needed to use virtual destructors. Thanks to everybody for helping me go from 16MB leaked per sound to leaking exactly 0 bytes in the whole program(there are other nested leaks that have nothing to do with my code but some DLLs I use).
Hulag is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help understanding a problem dnguyen1022 C++ Programming 2 04-29-2009 04:21 PM
Memory problem with Borland C 3.1 AZ1699 C Programming 16 11-16-2007 11:22 AM
Someone having same problem with Code Block? ofayto C++ Programming 1 07-12-2007 08:38 AM
A question related to strcmp meili100 C++ Programming 6 07-07-2007 02:51 PM
WS_POPUP, continuation of old problem blurrymadness Windows Programming 1 04-20-2007 06:54 PM


All times are GMT -6. The time now is 12:02 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22