Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete object constructed via placement new operator?

Tags:

char * buf = new char[sizeof(T)];
new (buf) T;
T * t = (T *)buf;
//code...
//here I should destruct *t but as it is argument of template and can be
//instantiated via basic types as well (say int) so such code 
/*t->~T();*/
//is incorrect (maybe correct? Strange, but it works on VS 2005 for basic types.)
//and this code 
/*delete t;*/ 
//crashes the program.
delete [] buf;

So what is correct way to destruct t?

P.S. The code above is only for describing my problem, and have not real relationship with code I'm going to write. So please don't give answers like (Why use placement new instead of non-placement? or something similar)

like image 385
Mihran Hovsepyan Avatar asked Jul 18 '11 08:07

Mihran Hovsepyan


People also ask

Is there a placement delete?

It is not possible to call any placement operator delete function using a delete expression. The placement delete functions are called from placement new expressions. In particular, they are called if the constructor of the object throws an exception.

Does placement new call destructor?

An explicit call to destructor is only necessary when an object is placed at a particular location in memory by using placement new. Destructor should not be called explicitly when the object is dynamically allocated because the delete operator automatically calls destructor.

How do you delete a new object in C++?

In C++, the single object of the class which is created at runtime using a new operator is deleted by using the delete operator, while the array of objects is deleted using the delete[] operator so that it cannot lead to a memory leak.

What is placement new operator in C++?

Placement new is a variation new operator in C++. Normal new operator does two things : (1) Allocates memory (2) Constructs an object in allocated memory. Placement new allows us to separate above two things. In placement new, we can pass a preallocated memory and construct an object in the passed memory.


2 Answers

...instantiated via basic types as well (say int) so such code
t->~T(); is incorrect
...

Wrong. That code is legal and correct in template code even if T can be a primitive type.

C++ standard: 5.4.2

5.2.4 Pseudo destructor call [expr.pseudo]

  1. The use of a pseudo-destructor-name after a dot . or arrow -> operator represents the destructor for the non-class type named by type-name. The result shall only be used as the operand for the function call operator (), and the result of such a call has type void. The only effect is the evaluation of the postfix expression before the dot or arrow.
  2. The left hand side of the dot operator shall be of scalar type. The left hand side of the arrow operator shall be of pointer to scalar type. This scalar type is the object type. The type designated by the pseudo destructor- name shall be the same as the object type. Furthermore, the two type-names in a pseudodestructor- name of the form ::opt nested-name-specifieropt type-name :: ˜ type-name shall designate the same scalar type. The cv-unqualified versions of the object type and of the type designated by the pseudo-destructor-name shall be the same type.
like image 131
Armen Tsirunyan Avatar answered Sep 30 '22 14:09

Armen Tsirunyan


You first destruct the object by directly calling the destructor:

t->~T();

Then you destroy the memory by calling delete[] on the pointer returned from new[]:

delete []buf;
like image 44
Nicol Bolas Avatar answered Sep 30 '22 13:09

Nicol Bolas