Re: Dipose vs Destructor



Jochen Kalmbach [MVP] wrote:
Hi Scott!

I have a C++ CLI Managed DLL project which is consumed by C# clients.

What is the recommended way for ensuring proper clean up of managed
resources and native resources in a managed C++ class?

The same as for C# assemblies: IDisposable

Except that C++/CLI doesn't permit you to implement IDisposable. To quote
from (an early draft of) the C++/CLI specification (8.8.8/25):

C++/CLI implements the destructor and finalizer semantics in any ref class T
by using the CLI dispose pattern, which makes use of five functions
(Dispose(), Dispose(bool), Finalize(), __identifier("~T")(), and
__identifier("!T")()), all of whose definitions are generated by the
compiler, as required. These cleanup mechanisms are hidden from the C++/CLI
programmer.

In C++/CLI, the proper way to do cleanup is to place all of the cleanup code
in the destructor and finalizer, as follows:
.. The finalizer should clean up any resources that are represented by value
types ([CD]: e.g. HANDLEs to native resources).
.. The destructor should do the maximal cleanup possible. To facilitate this,
the programmer should call the finalizer from the destructor and write any
other cleanup code in the destructor. A destructor can safely access the
state of ref classes with references from the object, whereas a finalizer
cannot.

For ref classes, both the finalizer and destructor must be written so they
can be executed multiple times and on objects that have not been fully
constructed.

-cd


.



Relevant Pages

  • Re: Delete and Garbage Collection
    ... > Adding a destructor with a cleanup statement. ... But a destructor of a __gc class is really a finalizer. ... So if you really care about the non-managed resources in a managed ... correct time is vital and can't be delayed until the finalizer. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Problem with C# not calling MC++ destrcutor
    ... referring to that as a Finalizer instead of a destructor? ... As far as cleaning up resources, is it acceptable to put my clean up ... would automatically dispose of all of the references it is holding. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problem with C# not calling MC++ destrcutor
    ... referring to that as a Finalizer instead of a destructor? ... In the proposed implementation of the IDisposable interface, the finalizer is calling ... You should always try to call Dispose if you can, as that is a more efficient way of freing up the resources. ... You should always try to free up all unmanaged resources as soon as possible. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Instantiating Managed structs and classes not as handles
    ... Also a destructor and!Nmethods should be defined ... any cleanup. ... finalizer is called by the GC for normal object instantiations via gcnew. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: system.drawing namespace question "Graphics"
    ... The finalizer (not destructor) is run when the garbage collector tries to collect the object. ... garbage collector performs a cleanup? ...
    (microsoft.public.dotnet.languages.csharp)