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

What is the lifetime of temporary function arguments? [duplicate]

Temporary objects are destroyed at the end of the full expression they’re part of. A full expression is an expression that isn’t a sub-expression of some other expression. Usually this means it ends at the ; (or ) for if, while, switch etc.) denoting the end of the statement. In your example, it’s the end … Read more

Can goto jump across functions without destructors being called?

Warning: This answer pertains to C++ only; the rules are quite different in C. Won’t x be leaked? No, absolutely not. It is a myth that goto is some low-level construct that allows you to override C++’s built-in scoping mechanisms. (If anything, it’s longjmp that may be prone to this.) Consider the following mechanics that … 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

Destructor parameters

Section §12.4 of C++0x draft n3290 has this to say about destructors: Destructors A special declarator syntax using an optional function-specifier (7.1.2) followed by ˜ followed by the destructor’s class name followed by an empty parameter list is used to declare the destructor in a class definition. (emphasis added) So no, destructors do not take … Read more

May a destructor be final?

May a C++ destructor be declared as final? Yes. And if so, does that prevent declaration of a derived class: Yes, because the derived class would have to declare a destructor (either explicitly by you or implicitly by the compiler), and that destructor would be overriding a function declared final, which is ill-formed. The rule … Read more

Time complexity of delete[] operator [duplicate]

::operator delete[] is documented on (which is sometimes frowned upon) as: operator delete[] can be called explicitly as a regular function, but in C++, delete[] is an operator with a very specific behavior: An expression with the delete[] operator, first calls the appropriate destructors for each element in the array (if these are of … Read more

Manually destroy C# objects

You don’t manually destroy .Net objects. That’s what being a managed environment is all about. In fact, if the object is actually reachable, meaning you have a reference you can use to tell the GC which object you want to destroy, collecting that object will be impossible. The GC will never collect any object that’s … Read more