From c++ FAQ: http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.9
Remember: delete p does two things: it calls the destructor and it deallocates the memory.
If delete deallocates the memory, then what’s the need of the destructor here?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You need to call the destructor in case there are other things that need to be done other than just de-allocating memory.
Other than very simple classes, there usually are.
Things like closing file handles or shutting down database connections, deleting other objects that are pointed to by members data within your object, and so forth.
A classic example is the implementation of a stack:
Now think of what would happen if the destructor was not called every time one of your stacks got deleted. There is no automatic garbage collection in C++ in this case so the
stackDatamemory would leak and you’d eventually run out.This requiring of a destructor to delete all its resources moves down the tree towards the basic types. For example, you may have a database connection pool with an array of database connections. The destructor for that would
deleteeach individual database connection.A single database connection may allocate a lot of stuff, such as data buffers, caches, compiled SQL queries and so on. So a destructor for the database connection would also have to
deleteall those things.In other words, you have something like:
Freeing the memory for the DB connection pool would not affect the existence of the individual DB connection or the other objects pointed to by them.
That’s why I mentioned that only simple classes can get away without a destructor, and those are the classes that tend to show up at the bottom of that tree above.
A class like:
has no real need for a destructor since the memory deallocation is all you need to do.
The bottom line is that
newanddeleteare opposite ends of the same pole. Callingnewfirst allocates the memory then calls the relevant constructor code to get your object in a workable state.Then, when you’re done,
deletecalls the destructor to “tear down” your object the reclaims the memory allocated for that object.