How to determine the destructor correctly

I am relatively new to C ++ (and generally programming), so please forgive me if the question is not entirely clear right away.

I have a program in which a certain number of objects of the inner class are created [let call this "class1"]. The program works fine, and the objects do what they should.

The problem I'm trying to solve now is this: these objects are not destroyed (and therefore no memory is allocated) until the program exits, but I need this memory earlier.

Among other members of the class are objects of other internally defined classes (which also have members that are objects of the third class).

My question is the following: how to correctly determine the destructor for class 1 objects so that all data is canceled and memory freed?

I found out (perhaps it was already obvious to you) that the destructor is kind of

class1::~class1(void) { } 

will not work (I defined similar destructors for all internally defined classes).

Reading around, I realized that my mistake may be that it is a destructor that does nothing. How to solve this?

Thanks to everyone who will answer / help / comment.

Federico

+4
source share
4 answers

In C ++, you need to free memory manually. There is no garbage collector. Obviously, you need to free the memory manually inside your destructor. If you allocated memory using the new one, you need to use delete for each resource that you allocated new inside the deconstructor, for example:

 class1::~class1(void) { delete resource1; delete resource2; etc... } 
+12
source

If you allocate memory dynamically, you need to free it in the destructor, but the best solution would be to use some smart pointer to store dynamic data - std::auto_ptr or std::shared_ptr . Then you do not need to explicitly free the memory - this will be done automatically in the smart pointer destructor.

+3
source

Memory on the stack

If your class does not have an allocated object heap, you do not need to explicitly define the destructor of your class. Created by the compiler should handle the task well, i.e. Call destructors for base class objects and member objects, etc.

Heap memory - manual memory management

If your class has a bunch of objects selected, you need to manually release them in your destructor. Make sure you correctly release them at all possible exit points , for example, handle exceptions and free up resources.

Heap Memory - RAII

Alternatively, you can use some well-defined RAII class for automatic resource management, for example scoped_ptr , scoped_array in Boost, shared_ptr in STL, etc.

+2
source

First you should try to allocate your objects on the stack:

 Object obj; 

so you don’t have to worry about memory allocation. The disadvantage is that the life of the object is limited by the current area.

If this is not good for you, you should consider using smart pointers. If this is not an option, use new / delete to create / destroy your objects. But you have to be careful, each new should lead to delete at a certain point in time.

+2
source

All Articles