Re: gcroot<> objects and finalization
Hi Kevin!
Class Managed (C++/CLI) holds a pointer to class Unmanaged (native C++).
Class Unmanaged in turn holds a gcroot< > pointer (to a TcpClient).
On process exit, the finalizer of "Managed" deletes the Unmanaged object.
This is ok.
The destructor of Unmanaged tries to use the gcroot<> object (to perform a
disconnect) and I seem to get an Exception that the gcroot object has
already been disposed (ObjectDisposedException).
If you are called from the finalizer, you must never use any .NET
objects! This is not allowed!
So I suggest you implement a "Dispose" Methode in your unmanaged class.
And I also suggest, you should implement the IDisposable-Pattern in your
managed class.
If the managed class is called from Dispose, then you should also
Dispose other managed classes and delete the unmanaged class.
If you are called from the finalizer, you should *only* delete the
unmanaged class and never ever use or reference managed classes!
So the question really comes down to what assurances exist at process exit
in terms of managed objects held by the gcroot<> template, and whether they
can be assumed to still exist.
Inside a finilizer, you must assume, that any other managed objects does
not exist anymore!
Or, does the fact that I have a finalizer
indirectly referencing another managed object (ie. via the native object)
mean I am violating the rule that a finalizer should not refer to other
managed objects (which is a definite no-no in a fully managed environment)
Exactly.
--
Greetings
Jochen
My blog about Win32 and .NET
http://blog.kalmbachnet.de/