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



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?

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. Shouldn't they
have their destructors called when "~MyCSharpClass" is called? . 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
.



Relevant Pages

  • 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: 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)
  • Re: Is Dipose( ) a contractual obligation, if you provide one?
    ... You're at least saying "it'd be a really good idea for you to call Dispose ... relinquish its resources at an explicit point." ... and the finalizer should take care. ... a UEntity unmanaged object requires a USession unmanaged ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Dispose must be thread-safe ?
    ... > If you add any reference to 'this' pointer in Dispose's code just after ... so that GC can reclaim resources early. ... > the managed class and must be accessed by 'this' pointer. ... > any point where 'this' is accessed inside of Dispose is guaranteed against ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Lowdown on finalization
    ... Because the method no longer referes to o, ... Finalize is a method of object. ... Dispose - release resources. ...
    (microsoft.public.dotnet.languages.vb)

Loading