Questions tagged [explicit-destructor-call]

Explicit destructor call is intended for questions related to programmatic deallocation of memory.

An explicit destructor call may be necessary in the following scenarios:

  • In languages with multiple inheritance, implicit deallocation may fail in multiple-inheritance situations where the type information does not correspond to the underlying type of the actual object.

  • In languages with manual memory management, to perform cleanup of data placed at a specific address in memory

References

34 questions
41
votes
2 answers

why is the destructor call after the std::move necessary?

In The C++ programming language Edition 4 there is an example of a vector implementation, see relevant code at the end of the message. uninitialized_move() initializes new T objects into the new memory area by moving them from the old memory area.…
28
votes
3 answers

Why can't constructors be explicitly called while destructors can?

In the following C++ code, I am allowed to explicitly call the destructor but not the constructor. Why is that? Wouldn't be explicit ctor call more expressive and unified with the dtor case? class X { }; int main() { X* x = (X*)::operator…
24
votes
4 answers

Explicit call to destructor

I stumbled upon the following code snippet: #include #include using namespace std; class First { string *s; public: First() { s = new string("Text");} ~First() { delete s;} void Print(){ cout<<*s;} }; int…
24
votes
3 answers

Is it legal to call member functions after an object has been explicitly destroyed but before its memory was deallocated?

I have this code: struct data { void doNothing() {} }; int main() { data* ptr = new data(); ptr->~data(); ptr->doNothing(); ::operator delete(ptr); } Note that doNothing() is being called after the object has been destroyed but…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
22
votes
2 answers

Is it allowed to call destructor explicitly followed by placement new on a variable with fixed lifetime?

I know that calling destructor explicitly can lead to undefined behavior because of double destructor calling, like here: #include int main() { std::vector foo(10); foo.~vector(); return 0; // Oops, destructor will be…
yeputons
  • 8,478
  • 34
  • 67
17
votes
1 answer

Pseudo-destructor call does not destroy an object

Consider the following code: #include typedef int t; t a=42; int main() { a.t::~t(); std::cout << a; //42 } I'm expected that a will be destroyed. But it is not true, why? How does do that pseudo-destructor call will be…
14
votes
4 answers

Do we need to explicitly call the destructor for the "simple POD classes" allocated with "placement new"?

Here by "simple", I mean a class with non-virtual empty destructor or POD type. Typical example: char buffer[SIZE]; T *p = new(buffer) T; ... p->~T(); // <---- always ? What happens if we don't call the explicit destructor on p? I don't think it…
iammilind
  • 68,093
  • 33
  • 169
  • 336
10
votes
3 answers

Can I call a virtual destructor using a function pointer?

I have class Data which can hold a pointer to an object. I want to be able to call its destructor manually later on, for which I need its address stored in a variable but it seems that taking the address of constructor/destructor is forbidden. Is…
7
votes
1 answer

Is reduction of `constexpr` object lifetime legal in C++?

For ordinary objects (even for const ones), it is permissible to end their lifetime by explicitly calling the destructor. Later, for example, the program can start another object lifetime in the same memory location using placement new. But is it…
Fedor
  • 17,146
  • 13
  • 40
  • 131
6
votes
1 answer

Is it safe to use placement new on 'this' pointer

Current Implementation I have a class containing unique_ptr fields which depend on one other: class ResourceManager { ResourceManager() {} ResourceManager(A* a_ptr) : b_ptr(new B(a)), c_ptr(new C(b_ptr.get())) {} ResourceManager&…
WaelJ
  • 2,942
  • 4
  • 22
  • 28
5
votes
1 answer

is it required to call a non-trivial destructor when it is a noop?

is it required by the standard to call non-trivial destructor when you know that in this specific case the destructor is a noop ? is the code likely to be broken by compliers if the destructor is not called ? the use case is a class that contain a…
4
votes
2 answers

Is calling destructor from a catch block in constructor safe?

In my constructor, I have to destroy any remaining resources if any code in it throws. I'd like to avoid writing duplicate code so I just call the destructor in the catch block which than frees any resource that has been created. Is this safe? I'm…
user7321642
3
votes
1 answer

Pseudo-destructor call with template keyword

The following code does not compile with clang 5.0.0 (compilation flags are -std=c++14 -Wall -Wextra -Werror -pedantic-errors -O0): struct foo { }; int main() { foo f; f.~decltype(f)(); // OK f.template ~decltype(f)(); // OK …
3
votes
1 answer

Access member variables after destructor call

i have code like this: class Foo { public: void blub () {} }; class Test { public: Foo& foo; Test (Foo& f) : foo (f) {} void test () { this->~Test (); foo.blub (); } }; After the explicit call to the destructor, all member…
Erlkoenig
  • 2,664
  • 1
  • 9
  • 18
3
votes
2 answers

After an object is destroyed, what happens to subobjects of scalar type?

Consider this code (for different values of renew and cleanse): struct T { int mem; T() { } ~T() { mem = 42; } }; // identity functions, // but breaks any connexion between input and output int &cleanse_ref(int &r) { int *volatile…
1
2 3