What destructors are run when the constructor throws an exception?

if a constructor throws an exception, what destructors are run? Destructors of all the objects completely created in that scope. Does it make any difference if the exception is during the initialization list or the body? All completed objects will be destructed. If constructor was never completely called object was never constructed and hence cannot …

Read more

How is it possible (if it is) to implement shared_ptr without requiring polymorphic classes to have virtual destructor?

Yes, it is possible to implement shared_ptr that way. Boost does and the C++11 standard also requires this behaviour. As an added flexibility shared_ptr manages more than just a reference counter. A so-called deleter is usually put into the same memory block that also contains the reference counters. But the fun part is that the …

Read more

Forward declaration with unique_ptr? [duplicate]

It’s explicitly legal. The rule is that the types used to instantiate a template in the standard library must be complete, unless otherwise specified. In the case of unique_ptr, §20.7.1/5 says “[…] The template parameter T of unique_ptr may be an incomplete type.” There are certain operations on the pointer which require a complete type; …

Read more

shared_ptr magic :)

Yes, it is possible to implement shared_ptr that way. Boost does and the C++11 standard also requires this behaviour. As an added flexibility shared_ptr manages more than just a reference counter. A so-called deleter is usually put into the same memory block that also contains the reference counters. But the fun part is that the …

Read more

GNU GCC (g++): Why does it generate multiple dtors?

First, the purposes of these functions are described in the Itanium C++ ABI; see definitions under “base object destructor”, “complete object destructor”, and “deleting destructor”. The mapping to mangled names is given in 5.1.4. Basically: D2 is the “base object destructor”. It destroys the object itself, as well as data members and non-virtual base classes. …

Read more

In C# what is the difference between a destructor and a Finalize method in a class?

Wikipedia has some good discussion on the difference between a finalizer and a destructor in the finalizer article. C# really doesn’t have a “true” destructor. The syntax resembles a C++ destructor, but it really is a finalizer. You wrote it correctly in the first part of your example: ~ClassName() { } The above is syntactic …

Read more

What is the difference between using IDisposable vs a destructor in C#?

A finalizer (aka destructor) is part of garbage collection (GC) – it is indeterminate when (or even if) this happens, as GC mainly happens as a result of memory pressure (i.e. need more space). Finalizers are usually only used for cleaning up unmanaged resources, since managed resources will have their own collection/disposal. Hence IDisposable is …

Read more

How to destroy an object?

You’re looking for unset(). But take into account that you can’t explicitly destroy an object. It will stay there, however if you unset the object and your script pushes PHP to the memory limits the objects not needed will be garbage collected. I would go with unset() (as opposed to setting it to null) as …

Read more