开发者

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?

Obje开发者_如何学运维ct* object = new Object(...);
...

delete object;

OR

object->~Object();

Second, does the delete operator call the destructor implicitly?


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.


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.


delete is preferred. Just calling the destructor does not free the memory allocated by the new.


Use delete. It calls the objects destructor and then frees allocated memory.

Also, it's not a deconstructor, but a destructor.


Normally you never want to explicitly call the destructor. Just use delete.


Invoking delete will invoke the destructor and then release the memory.

Invoke the destructor explicitly will only invoke the destructor, and not release the memory.

You should therefore almost always call delete: except when you want to invoke the destructor without releasing the memory, e.g. because you constructed the object using placement new.


You should never call the destructor yourself. delete will call it for you


Something else to consider:

since delete calls the destructor internally, it’s an error to do both, i.e. calling the destructor and then delete. So the following code:

Foo* px = new Foo;
// …
px->~Foo();
delete px;

Will produce a nasty bug. Depending on the actions taken in the actual destructor, this may go unnoticed for quite some time, since the compiler actually allows this code. This may lead to subtle, hard to discover bugs.


You should use delete

http://www.parashift.com/c++-faq-lite/dtors.html


When you are using dynamic memory allocation to create the object at that time you can use the delete operator to destroy/delete the object but when you are not using DMA at that time trying to delete an object using delete operator throws error. One way to handle this situation is, writing your own destructor explicitly and calling the destructor using the object to destroy it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜