When I'm installing a class I need to perform a 'delete' after using the object even though the class has a destructor?

1

If I instantiate a class and render an object from it, I have to deallocate the memory of that object at the end of its use, right? But if the class that gave rise to my object has a destructor, will it be necessary to perform a delete on the object even though the class with its destructor already does the memory deallocation work?

    
asked by anonymous 10.09.2017 / 19:35

1 answer

1

It depends.

If you allocate stack do not need it, this is an automatic memory and the compiler generates code itself that will deallocate whatever it takes.

If the allocation is in heap you need to know if the class does this for you, if it does, you do not have to do anything. If you wrote the class you need to analyze whether it is not the case to include memory management within it. The destructor may indicate that you release the memory, but this is not guaranteed, you need to see the documentation. If it does you should not release the memory manually.

If you're in heap , you can use a smart pointer, so he'll deal with you. It is most appropriate whenever the class lets the programmer take care of memory management.

If none of this is done, yes, you need to deallocate the memory under penalty of causing a memory leak. It is not easy to do right in many cases, and even in the trivial it is easy to commit some slip.

    
10.09.2017 / 19:41