Re: Problem with C# not calling MC++ destrcutor



Jeremy Chaney wrote:
Lebesgue wrote:
I'm pretty surprised to hear that it is possible for my Finalizers to
not be called at shutdown. If I am being required to manually free up my
resources, what is the point of GC?
GC is here to clean up unreferenced _memory_. You should deal with other (unmanaged) resources using the dispose pattern in C#. There has been a long thread recently (named C++/CLI is the way to go) where a reference counted model for disposing unused resources in C# has been proposed.

Can you show us your finalizers? My guess is you are trying to use them the way they were not meant to be.

I think I'm sinking in over my head here... :) When you say Finalizers,
do you mean my "~MyClass" method?

Yes, he does.

Here it is:

~MyCSharpClass()
{
foreach (KeyValuePair<Char, MyManagedCPPClass> kvp in m_MyMap)
{
kvp.Value.Dispose();
}
m_myotherManagedCPPClass.Dispose();
}


The need for this destructor (I'm going to call it that until someone
tells me what to call it in C#) is surprising to me. The objects that I
have to call Dispose on are both Managed C++ objects.

As you use objects that implement IDisposable, your class should also implement IDisposable, so that the resurces can be freed safely and as soon as possible.

If you are using the finalizer to free the resources, you have absolutely no control over when it happens. If you implement IDisposable, you can call the Dispose method when you want to free the resources.

Shouldn't they
have their destructors called when "~MyCSharpClass" is called?

No. After your finalizer has run, there will be no references to the child objects. At the next garbage collection they will be placed in the queue of object to be finalized, and a background process will call their finalizers eventually.

If you implement IDisposable (with Dispose containing the code that you currently have in the finalizer), it will make the memory management much more efficient. After you have called Dispose, there are no finalizers to handle, and the garbage collector can free the memory of your object and all it's child objects at the same time.

Before
I added the calls to Dispose, "~MyCSharpClass" was always called, but
the destructors for my Managed C++ objects were not. (I fixed this with
the addition of Dispose).

Thanks,
--Jeremy


--
Göran Andersson
_____
http://www.guffa.com
.



Relevant Pages

  • Re: basic question...destructors confusion
    ... I *always* implement IDisposable even if I am not using unmanaged resources ... Dispose() instead of using finalizers. ... Protected Overrides Sub Finalize() ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Delphi and the .Net platform
    ... The only reason for using the Delphi Free is to ensure unmanaged resources are cleaned up. ... Any component classes derive from classes that already implement IDisposable and, because they are managed by forms that own them, the call to Dispose is managed automatically by the form. ... The false premise of leaving calls to Free in legacy code when it is ported to .NET will lead to errors in GC management, giving the lesser experienced reason to believe that they are still wotking with a deterministic memory model. ... As for Java's restrictions, there are times when you could really do with declaring just a tad more than a sinlge one line interface declaration in one unit, e.g. ...
    (borland.public.delphi.non-technical)
  • Re: Problem with C# not calling MC++ destrcutor
    ... finalizers (or call them destructors) don't call finalizers on instance ... IDisposable interface and from its Dispose() method, ... up the resources used by the object. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Clean Up managed resources
    ... //Clean up unmanaged resources ... If somebody called Dispose on your object, ... then they have their own finalizers which will be run automatically ... The garbage collector will tell each managed class to ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: what are unmanaged resources
    ... the GC will come along and clean up all the memory and resources associated ... in a deterministic fashion and clean up unmanaged resources. ... placed inside the Dispose functions. ... sooner than later (i.e. by waiting for the garbage collector) and you should ...
    (microsoft.public.dotnet.languages.csharp)