简体   繁体   中英

Why call the default destructor of an object before freeing the memory allocated to that object?

I seen some code do the following:

ExampleObject< T > * eo = const_cast< ExampleObject< T > * >(this);
eo->~ExampleObject();
free( eo );

The ExampleObject in question has been allocated using placement new. There is no user-defined destructor supplied, so it's the compiler supplied default destructor being used here, I think.

I don't understand why it is necessary to call the destructor here. If we had a user-defined destructor that de-allocated memory for some of it's class members I could understand the usage here, but in the case of a default destructor I don't know why we'd do this.

What does the default destructor of an object do, that would require us to call it before freeing the allocated memory for the object?

What does the default destructor of an object do

It calls the destructors, if any, for the object's data members.

that would require us to call it before freeing the allocated memory for the object?

The ONLY time a destructor should ever be called explicitly is when the object has been constructed using placement-new inside a pre-existing memory block. Using placement-new separates the tasks of object construction/destruction from memory allocation/deallocation, so you need to construct and destruct an object explicitly, but you don't have to allocate/deallocate its memory block, you can manage that however you want elsewhere.

If you do not use placement-new to construct the object, but rather allocate + construct the object using new instead, then you must destruct + deallocate the object using delete (and you should preferably use a smart pointer, std:unique_ptr or std::shared_ptr , to handle that for you).

If you do not construct the object using any form of new , then DO NOT try to destruct the object manually at all. The object is in automatic storage and the compiler will manage it for you.

The C allocation functions know nothing about C++ objects. Unlike new and delete that allocate the memory needed and call the constructor/destructor, all The C functions do is allocate/deallocate a suitably sized amount of memory that you can use.

So when you use the C functions, you have to call the allocation function to get the memory, call placement new on it to actually construct the object in the memory (this is required to actually have an object of that type). Then, when you are done with it you need to destroy the object by manually calling the destructor (you need to do this so the objects lifetime properly ends) and then pass the pointer to free so the memory can be released.

This is why you should not be using *alloc and free in C++. They require a lot of extra work and are not type safe.

A default destructor function is called automatically when the object goes out of scope:

  1. The function ends
  2. The program ends
  3. A block containing local variables ends
  4. A delete operator is called

Default destructor always invokes the destructors of its member objects (not for member variables that are pointers to objects). If we allocate memory dynamically it should be handled by us, otherwise default destructor clears the local variables.

If we do not write our own destructor in class, the compiler creates a default destructor for us. The default destructor works fine unless we have dynamically allocated memory or pointer in class. When a class contains a pointer to memory allocated in class, we should write a destructor to release memory before the class instance is destroyed. This must be done to avoid memory leak.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM