Jump to content

Destruction of allocated memory


Laurens
 Share

Recommended Posts

Hi,

 

This is probably a real simple question but I was unable to find a conclusive answer searching the web for a bit.

 

I have a singleton that manages some pointers to objects that have been new'ed at some point.

 

Now, if my game has a clean exit, will the O/S (Windows/Linux/Mac) automatically reclaim the memory that my game was using without me having actually deleted the singleton and the pointers it manages?

 

And what if my game crashes? Will the O/S still reclaim the memory?

 

Thanks!

Link to comment
Share on other sites

Yeah, the OS will clean up the memory, even if the game crashes, but it's not a good way to do things. Write your code so that it doesn't crash and delete the memory yourself.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

The biggest problem when not freeing your classes with delete, is that their destructors will never get called. So if you do some file I/O in the destructor (like deleting temp files), they will never get deleted, so it's not always only a issue about freeing the memory.

 

Why do you think you have thousand of temp files in your temp folder? :(

They are mostly remains from crashed programs, or not responding programs which you had to terminate from the process list in task manager.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

The object in question does not open any files or anything so that would have not been a problem, but to ensure some future requirement won't change this I opted to no longer use singletons but instead use the service locator pattern that does give me control over when I want to delete the managers.

 

Cheers!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...