deleting pointers in C++

Started by
14 comments, last by Zahlman 17 years, 1 month ago
I was under the impression that anytime you use the keyword new when instantiating a class, you should use delete to deallocate the memory. I have an Application class that has pointers to another class, State. The Application is constructed and destructed correctly, but the State object is created but never destroyed. Is it automatically destroyed when the program is closed? If so, then shouldn't the destructor for the Application and State class get called? Here is some pseudocode of what I have.

Application* g_pGameApp;
Main()
g_pGameApp = new Application();
g_pGameApp->Init()
 {
  State* Menu = new State()
 }
g_pGameApp->Run()
 {
  //stuff happens each frame
 }
g_pGameApp->Shutdown()


If I try and delete the State in the Application Shutdown it crashes.
Advertisement
I think because Menu is limited to Run.

Do this:

State* Menu; in the classs, then in Run do: Menu = new State();
Well, in in class declaration, make a member, called Menu of type State*, so:

class Application{  State* Menu;  //methods}
You dont need to delete anything right before the program crashes, windows will take care of that. You only need to worry about new and delete when your dealing with memory being created and deleted at run-time.

"If so, then shouldn't the destructor for the Application and State class get called?"

-No, windows takes care of your program in RAM. It knows nothing of your code or classes, it will just free up the memory taken by your application.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

@crazyfool
Thanks for the quick replies. I guess my pseudo code was written poorly. The State pointer is a member of the application class. It is instantiated in the Init function, and then manipulated in Run().
I imagine it's crashing because you are possibly trying to use state after deleting it. Why not just delete the State* in the destructor for the Application class, and delete the application class that you created at the end of the program, after you call it's shutdown method. You should really delete your pointers in the reverse order in which you created them, to prevent trying to use a dead pointer.
Quote:Original post by localrob2
I was under the impression that anytime you use the keyword new when instantiating a class, you should use delete to deallocate the memory.

This is absolutely almost always true. An exception might be, for example, in the case of a "smart" pointer that does the delete for you. Put the delete back in and run your application in the debugger to find out where and why it is crashing.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote:Original post by dpadam450
You dont need to delete anything right before the program crashes, windows will take care of that. You only need to worry about new and delete when your dealing with memory being created and deleted at run-time.

"If so, then shouldn't the destructor for the Application and State class get called?"

-No, windows takes care of your program in RAM. It knows nothing of your code or classes, it will just free up the memory taken by your application.


That's all well and good, but you need to remember that Windows will only free the memory. Destructors will not be called. Also, the behavior you're describing is platform dependent. Some operating systems will not free up memory for you.

Just because you can get away with it doesn't mean you should.


jfl.
If you can, run the code in debug mode and try and find the line on which the crash occurs. It will help determine if you're deleting a garbage-pointer, deleting a null-pointer, or using a pointer post-deletion.

Quote:Original post by jflanglois
That's all well and good, but you need to remember that Windows will only free the memory. Destructors will not be called. Also, the behavior you're describing is platform dependent. Some operating systems will not free up memory for you.
Just because you can get away with it doesn't mean you should.

Agreed.
The RAII paradigm lends itself very strongly to C++ development, and under this paradigm destructors do a lot more than just freeing up RAM.
If you create an object at runtime using new it allocates a load of memory to store the object then calls the constructor. If you don't call delete when you are done the memory will not be freed and destructors wont be called. Most modern operating systems will clean up after you at the end of the program but that doesn't help in this situation:

void Class::DoSomething() {     for (int i = 0; i < 10; ++i) {              m_Object = new Object();              m_Object->DoSomethingElse()     }}


That would create 10 new objects, and at the end of the function m_Object will only point to the last one. In this instance you will have NO way of deleting the other 9 objects you created. Therefore you just leaked 9 * sizeof(Object)s worth of memory.

This would be much better (still not great coz you wouldn't want to create objects on the heap for this but its just an example)

void Class::DoSomething() {     for (int i = 0; i < 10; ++i) {              m_Object = new Object();              m_Object->DoSomethingElse()              delete m_Object;              m_Object = 0;     }}



You don't NEED to set m_Object to zero once deleted, but it does allow you the opportunity to check and see if it points somewhere:

if (m_Object != 0) {
//Object exists
}

Also if you use new you need to use the right version of delete:

Object  *obj = new Object()delete obj;Object *obj = new Object[10]; //Creates 10 objects in consecutive memorydelete [] obj; //Deletes all 10 objects and calls their destructors


Luke.
Member of the NeHe team.

This topic is closed to new replies.

Advertisement