How do I correctly destruct a derived object that was constructed using placement new

unichips

Say we have a C++ program with this sort of class inheritance:

class A {
public:
   virtual ~A() {/* ... */}
};

class B : public A {
public:
   virtual ~B() {/* ... */}
};

class C : public A {
public:
   virtual ~C() {/* ... */}
};

And furthermore, there are specialized memory constraints which requires that B and C must always be allocated in a special region of RAM (e.g. a reserved region of physical SRAM that guarantees faster response times than normal SDRAM) and so we must never allocate instances of B or C from the general heap. So we might have something like:

A * ptr;

if(condition) {
   ptr = specialized_allocator(sizeof(B));
   new(ptr) B;
} else {
   ptr = specialized_allocator(sizeof(C));
   new(ptr) C;
}

/* Do something, which persists beyond the scope
   of the function where allocation occurred... */

ptr->~A();
specialized_deallocator(ptr);

In this scenario, will the complete chain of derived class destructors be invoked correctly, or will it end up only invoking the top-level A destructor?

Solved Games

Run this and it may help a little:

#include <iostream>

class A {
public:
    virtual ~A() { std::cout << "A\n"; }
};

class B : public A {
public:
    virtual ~B() { std::cout << "B\n"; }
};

class C : public A {
public:
    virtual ~C() { std::cout << "C\n"; }
};

int main()
{
    A* ptr = new C(); // or A* ptr = new B()
    // ...
    ptr->~A();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How To Destruct Destructor-less Types Constructed via 'Placement New'

Do i need to delete a object that was constructed with new and placement

Do I leak the constructed object when using new to create an object whose constructor throws?

How do I destruct a dynamically allocated array of dynamic object?

How are contents of an object defined when using placement new on existing object

UML Class Diagram: How do I correctly represent a vector of derived classes using their base class?

How can I destruct this object and mapping in reactjs?

placement new with derived class

how do you delete an object allocated with placement new

Zend: How to correctly destruct a custom object in PHP 7?

How to determine if object has been placed using placement new

Is modifying the internal bytes of a const object undefined behavior in case it contains another object constructed by placement new?

How Do I Correctly Set a getElementById Object?

How do I correctly clone a JavaScript object?

How do I use map function, so I can add the index as a property to the newly constructed object?

In C#, how do I serialize my derived object to this XML?

Can I add to a vector using placement new

How do I insert a new object into object and unwrap this new object?

Is it practically OK to delete object not constructed using the new expression?

How do I write tactics that behave like "destruct ... as"?

How do I extract a table from a webpage using selenium when the table is not constructed with the HTML 'table' tag?

Why do I need to specify the type of a default constructed object in this situation?

How do I append to a new object?

Can I overwrite a const object via placement-new?

How do I correctly iterate through an object in render?

How do I print every object in an arraylist and correctly count their quantity?

How do I correctly clean up a Python object?

How do I correctly create a related object overriding the save() method?

How do I correctly pass a self reference of one object to another?

TOP Ranking

HotTag

Archive