user6858405
user6858405

Reputation:

Destructors and the delete() method in C++

So I added a destructor function to a class in C++.

class object { ~object() };

And declared an object with the new method, thereby allocating it on the heap

object *pointer = new object;

Do I still need to use the

delete(object); 

method at the end of the program? (Isn't the destructor already responsible for exactly this?)

Upvotes: 1

Views: 12351

Answers (7)

Hayt
Hayt

Reputation: 5370

You have 2 different kinds of memory. The stack and the heap.

Everything on the stack deletes itself when you go out of scope (one of the steps of deletion is calling the destructor)

Everything which is on the heap (you call malloc, new, etc) you have to delete explicitly by yourself (which will then result in the destructor being called).

Smart pointers like unique_ptr / shared_ptr are a modern c++ way to get rid of the manual deletion on heap objects and to make sure they get deleted when the objects are not needed anymore.

Upvotes: 1

user2249683
user2249683

Reputation:

A new and a delete expression (which are not the new and delete operators) are invoking two calls:

new:

1) call the operator new for memory allocation
2) invoke the appropriate constructor

delete:

1) invoke the destructor
2) call the operator delete for memory deallocation

Hence you have to pair any new with a delete, to avoid resource (memory) leaks. However, object construction and destruction does not require new and delete.

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

Do I still need to use the delete(object); method at the end of the program?

Yes.

Isn't the destructor already responsible for exactly this?

No.

Your delete ptr; is what invokes that destructor. If you hadn't declared a destructor, one would have been declared for you, so that made no difference.

You don't need to write delete ptr; when your object has automatic storage duration (very loosely: "when you didn't create it with new"), because then the equivalent is done for you.

But in both cases the destructor is called.

Upvotes: 0

msc
msc

Reputation: 34598

Yes, you must need Delete method.

Default destructors call destructors of member objects, but do NOT delete pointers to objects. Thus, You need to write destructors that explicitly call delete. Like,

delete pointer;

Upvotes: 0

Alexey Guseynov
Alexey Guseynov

Reputation: 5304

You still must call delete(object). Destructor is responsible for how to delete an object and delete(object) is responsible for when to delete it. But in modern C++ usage of naked pointers is considered a really bad practice. You should consider using smart pointers, such as std::unique_ptr for managing memory.

Upvotes: 2

merl
merl

Reputation: 162

The destructor gets called when the objects lifetime is over. For heap allocated objects this means when you delete them. So a call to

delete(object);

calls your descructor and frees the allocated memory.

Upvotes: 0

krzaq
krzaq

Reputation: 16421

Destructors are a way of customizing the clean-up process (add logging, provide cleanup as per rule of three/five, etc.). You still need to delete, otherwise you'll have a memory leak.

That being said, it's frowned-upon and considered a bad practice to write code that actially needs news and deletes (especially the latter). If you need to store an object on the heap, use unique_ptr/shared_ptr or a container like vector.

If you have the time, I suggest you watch Herb Sutter's talk from C++Con 2016: Leak-Freedom in C++ for guidance.

Upvotes: 0

Related Questions