Jump to content
  • Advertisement
Sign in to follow this  
Jiia

Manually calling destructors

This topic is 4848 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

I have an object in global space. Some of it's members have allocations which are freed on destruction. But I also have a memory manager that alerts me to leaks or other errors, and this is summed up at the end of my Main() function. The problem is that my global space object won't have it's destructor called until after main has exited, and my memory manager will most likely throw a fit, thinking all of that data is going to be left in memory. I could create a Free() type function for the global object. But it would be a hassle freeing each attribute in that function, where the destructor handles it all automatically. So is it a problem to call Object.~ClassName(); at the end of main? Or is that just a bad idea? I would hate to have to allocate this object in main, but if this is a problem, that's most likely what I'll do. I've never called destructors manually, so I don't know if some whacky memory swipe routine will chew up my application or not. Do destructors do anything other than the code you specify in them? I mean are there any hidden routines running in between the lines? Thanks for any help edit: Just in case it makes a difference, the global object is a simple structure. It actually doesn't even define it's own destructor. So the built in one would be called.

Share this post


Link to post
Share on other sites
Advertisement
You could put all of the deallocation/freeing into a function called CleanUp()and call it before main exits

thats the simplest thing i can think of

Share this post


Link to post
Share on other sites
Is this global object created on the heap?

If not then the memory manager should ignore it.

Does it allocate heap memory. I'm guessing not since it doesn't have a destructor. If it does can you not use smart pointers (std::auto_ptr or std::tr1::sharded_ptr/boost::shared_ptr) to automatically handle any memory deallocation.

It is hard to come up with suggestions without seeing an example of the structure and examples of how/when it's allocated.

You mention calling the objects destructor manually (complier provided)...then why not just call delete()?

You also mention that the destructor automatically handles freeing attributes, but they don't automatically deallocate heap allocated memory, if this is the case then you'll need to provide your own destructor.

Hope you get it sorted.

Share this post


Link to post
Share on other sites
Doesn't sound like a good idea: C++ FAQ Lite (says you should only explicitly call the destructor if the object was created via placement new)

If you explicitly call the destructor yourself, C++ is automatically going to call the destructor again anyway once the global variable gets destroyed automatically. Irrespective of what "kind" of destructor it is and what sort of class, calling a destructor twice on the same object isn't good.

I'm not sure what you should do as an alternative though, some more knowledgeable people than I should stop by in a bit. [wink]

EDIT: garyfletcher's suggestion seems like it should work.

Share this post


Link to post
Share on other sites
Look up "atexit()"...

Register a void(*)(void) function to clean up your objects as the CRT exits your app.

That's also a good way to clear static vectors of smart pointers.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jiia
I could create a Free() type function for the global object. But it would be a hassle freeing each attribute in that function, where the destructor handles it all automatically.


Why not just rename the current destructor to Free(), and redo the destructor to call that instead? (this is a common refactoring - stream dtors call .close() - and the equivalent is often used for constructors as well.)

Quote:
edit: Just in case it makes a difference, the global object is a simple structure. It actually doesn't even define it's own destructor. So the built in one would be called.


Oh. :) Um, there's also the option of refactoring everything such that it's not global any more (by passing it around - not by blindly making a singleton :P ). Or you could make a global pointer to the object, new it at the beginning of main, and delete it at the end (and fix the rest of the code to handle the level of indirection). Of course that may take a performance hit :/

Share this post


Link to post
Share on other sites
Sorry for not being clear. The object doesn't allocate any data. It's just a structure of settings that apply to the whole engine. Video settings, sound settings, input settings, datafile settings, etc. It's purpose is to let the game communicate with the engine without actually doing so, just by modifying the structure.

A few of the settings are objects which allocate their own data. That data is what is freed on destruction. Creating a CleanUp() type function is fine, it's just a lot of maintenance work when things are added and removed. Work that is normally automatic with the destructor. I believe I'll just allocate the data in main via a global pointer.

Thanks for the suggestions

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!