Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting an object

First, when you want to free the memory assigned to an object in C++, which one is preferred? Explicitly calling destructor or using delete?

Object* object = new Object(...);
...

delete object;

OR

object->~Object();

Second, does the delete operator call the destructor implicitly?

like image 390
Nima Avatar asked Jun 03 '10 18:06

Nima


2 Answers

delete implicitly calls the destructor, you don't need (more precisely: shouldn't) call it directly.

A destructor will never release the memory occupied by the object (It may reside on the stack, not on the heap, and the object has no way of knowing -- however, the destructor will delete any memory allocated by the object's components).

In order to free the memory of an object allocated on the heap, you must call delete.

When you write your own class, C++ will provide a default destructor to free the memory allocated by component objects (such as a QString that is a member of your class), but if you explicitly allocate memory (or other resources) in your constructor, be sure to provide a destructor that will explicitly free these resources.

Another general rule regarding your own classes: If you mark any methods virtual, your destructor should be virtual, as well (even if you rely on the default destructor), so that the correct destructor is called for any classes derived from yours.

like image 174
Tony the Pony Avatar answered Oct 06 '22 00:10

Tony the Pony


I prefer neither.

An explicit destructor call is needed very, very rarely, only when you are dissociating memory allocation from object lifetime. You might need it if implementing a custom container class.

An explicit delete is, potentially, a legitimate way to destroy an object dynamically created with a new expression but it should be unnecessary in most application code as it signals a place where potential mismatches between new and delete might occur and areas with potential exception safety issues.

Where an object lifetime is constrained to a block a local variable should normally be preferred as the memory allocation overhead is usually lower and the object will automatically be cleaned up correctly however the block is exited.

{
    // ...
    Object object( ... );

} // object destructor run, however this block is exited.

If there is some reason that this can't be need (e.g. the object has an excessive static size) or it's lifetime can't be matched to a particular scope, then usually some sort of smart pointer should be used to manage the objects lifetime. The most basic smart pointer which is available in standard C++ is std::auto_ptr which can be used for block scoped dynamically allocated objects but has 'surprising' behaviour on copy and assignment. Something like tr1::shared_ptr (or boost::shared_ptr) are common alternatives where shared ownership is needed.

{
    std::auto_ptr<Object> object(new Object(...));
    // ...
} // *object destructor run, however this block is exited.
like image 26
CB Bailey Avatar answered Oct 05 '22 23:10

CB Bailey