Why base class destructor (virtual) is called when a derived class object is deleted?

The Standard says After executing the body of the destructor and destroying any automatic objects allocated within the body, a destructor for class X calls the destructors for X’s direct non-variant members,the destructors for X’s direct base classes and, if X is the type of the most derived class (12.6.2), its destructor calls the destructors … Read more

Are destructors run when calling exit()? [duplicate]

No, most destructors are not run on exit(). C++98 §18.3/8 discusses this. Essentially, when exit is called static objects are destroyed, atexit handlers are executed, open C streams are flushed and closed, and files created by tmpfile are removed. Local automatic objects are not destroyed. I.e., no stack unwinding. Calling abort lets even less happen: … Read more

Why is my exception-in-destructor not triggering std::terminate?

std::terminate will be called if an exception is thrown during stack unwinding. That means that if an exception is called while another exception is being handled, then std::terminate will be called. In your example, that doesn’t happen – A(); will construct and immediately destroy an instance of A. The throw 30 will then be caught … Read more

Destruction of return value on destructor exception

As per the standard this behavior is wrong and this has already been mentioned in the comments section of the question. This is stated in the section on Exception handling. As per the defect reports at open-std.org, they have been aware that implementations (GCC and Clang) were wrong about this as early as 2015-09-28. But … Read more

Python __enter__ / __exit__ vs __init__ (or __new__) / __del__

There are several differences you appear to have missed: Context manager get a chance to provide a new object just for the block you are executing. Some context managers just return self there (like file objects do), but, as an example, database connection objects could return a cursor object tied to the current transaction. Context … Read more

C++ destructor with return

No, you can’t prevent the object from being destroyed by return statement, it just means the execution of the dtor’s body will end at that point. After that it still will be destroyed (including its members and bases), and the memory still will be deallocated. You migth throw exception. Class2::~Class2() noexcept(false) { if (status != … Read more

The difference between a destructor and a finalizer?

1) Is there a well-defined difference between “destructor” and “finalizer” as used in industry or academia? There certainly appears to be. The difference seems to be that destructors are cleanup methods that are invoked deterministically, whereas finalizers run when the garbage collector tells them to. 2) In that case, the C# spec gets it wrong … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)