Hasani said:
I'm assuming the the following happen behind the scenes.
First note that in the old syntax, deterministic cleanup is not available.
That is a feature of the new syntax. I'll answer your questions in terms of
the old syntax, and separately call out anything interesting about the new
syntax.
Also, note that in the old syntax ~Class maps to the finalizer of the class
(even though it generates a __dtor method). In the new syntax, ~Class maps
to Dispose (which is the destructor), and !Class maps to the finalizer.
1) f.Dispose() will be called automatically in the same thread context in
which f was initialized and no exception handling being performed if
f.dtor() throws an exception.
Note that F (being a reference class) cannot be instantiated on the stack in
the old syntax. So, if it were created with a __gc pointer, the old syntax
would still allow F:
ispose to be called from the finalizer thread (which
is different than the thread f was created on).
The new syntax does allow F to be created on the stack, in which case its
destructor (i.e. Dispose) will be called at the end of the scope even if an
exception takes place. Thus, the finalizer should never run.
2) f.dtor() [same text as 1]
In the old syntax, if Dispose is not run, the finalizer will. If Dispose is
run, it is very likely that the finalizer will not.
In the new syntax, the C++ generated destructor suppresses finalization.
3) there is no excpetion handling performed when f.Dispose() and the
f.dtor() is automatically called.
The question doesn't make sense. Perhaps you are asking, in the presence of
an exception, does Dispose and dtor get called?
If that is the question, the answer is no (both of the them cannot be
called). Depending on what try/__finally blocks you have in the old syntax,
the Dispose may run. If it does, the finalizer probably will not. If Dispose
doesn't run, the finalizer will run.
In the new syntax, if the type is allocated on the stack, then it will have
its destructor (i.e. Dispose) called if an exception occurs.
4) the private variable f._socket will not be finalized instantly when
variable, f, goes out of scope. It will be finalized the next time garbage
collection is performed by either the system, or a call to GC::Collect.
That is correct. If the destructor does not clean up the Socket (i.e. call
the Socket's destructor), the Socket will be finalized.
5) When I say automatically, this is done at compile time and not runtime.
the compiler adds code to do 1, 2 and 3. 4 would be at runtime.
Yes, the compiler does all of the above in new syntax. If you are using the
old syntax, it does not do this.
Also, if an object contains a finalizer, such as the Regex class. In the
next version of c++, will finalizers be treated as destructors so I can
do
No. Finalizers and destructors are two different things. A finalizer gets
called when a destructor was not called. We map destructors to the Dispose
method from IDisposable. So, if you define a destructor in any ref class,
C++ will generate the Dispose pattern for you. It will also chain
destructors as usual. So, using the delete keyword on an object will
ultimately cause the Dispose method to be called.
Because the C++ compiler generates Dispose for you, you cannot write Dispose
yourself. Instead, you need to use destructor syntax.
Hope that helps!