Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly free the memory allocated by placement new?

I've been reading somewere that when you use placement new then you have to call the destructor manually.

Consider the folowing code:

   // Allocate memory ourself char* pMemory = new char[ sizeof(MyClass)];  // Construct the object ourself MyClass* pMyClass = new( pMemory ) MyClass();  // The destruction of object is our duty. pMyClass->~MyClass(); 

As far as I know operator delete normally calls the destructor and then deallocates the memory, right? So why don't we use delete instead?

delete pMyClass;  //what's wrong with that? 

in the first case we are forced to set pMyClass to nullptr after we call destructor like this:

pMyClass->~MyClass(); pMyClass = nullptr;  // is that correct? 

BUT the destructor did NOT deallocate memory, right? So would that be a memory leak?

I'm confused, can you explain that?

like image 717
codekiddy Avatar asked Jan 18 '12 23:01

codekiddy


People also ask

Can you free memory allocated with new?

You must use delete operator to deallocate memory when it is allocated by new operator. no, you should use delete[] for new[], and delete for new.

How do you free the allocated memory?

Answer: You can use Free() function. Answer: C free() Dynamically allocated memory created with either calloc() or malloc() doesn't get freed on its own.

Does placement New allocate?

Because placement new does not allocate memory, you should not use delete to deallocate objects created with the placement syntax.

What function should be used to free the memory allocation?

Explanation: free() is used to free the memory spaces allocated by malloc() and calloc().


1 Answers

Using the new expression does two things, it calls the function operator new which allocates memory, and then it uses placement new, to create the object in that memory. The delete expression calls the object's destructor, and then calls operator delete. Yeah, the names are confusing.

//normal version                   calls these two functions MyClass* pMemory = new MyClass;    void* pMemory = operator new(sizeof(MyClass));                                    MyClass* pMyClass = new( pMemory ) MyClass(); //normal version                   calls these two functions delete pMemory;                    pMyClass->~MyClass();                                    operator delete(pMemory); 

Since in your case, you used placement new manually, you also need to call the destructor manually. Since you allocated the memory manually, you need to release it manually.

However, placement new is designed to work with internal buffers as well (and other scenarios), where the buffers were not allocated with operator new, which is why you shouldn't call operator delete on them.

#include <type_traits>  struct buffer_struct {     std::aligned_storage_t<sizeof(MyClass), alignof(MyClass)> buffer; }; int main() {     buffer_struct a;     MyClass* pMyClass = new (&a.buffer) MyClass(); //created inside buffer_struct a     //stuff     pMyClass->~MyClass(); //can't use delete, because there's no `new`.     return 0; } 

The purpose of the buffer_struct class is to create and destroy the storage in whatever way, while main takes care of the construction/destruction of MyClass, note how the two are (almost*) completely separate from each other.

*we have to be sure the storage has to be big enough

like image 54
Mooing Duck Avatar answered Oct 02 '22 13:10

Mooing Duck