Re: Problem with C# not calling MC++ destrcutor
- From: "Lebesgue" <lebesgue@xxxxxxxxx>
- Date: Sat, 3 Feb 2007 00:03:30 +0100
The need for this destructor (I'm going to call it that until someoneActually, there's no need for it in the form you have posted. See below how
tells me what to call it in C#) is surprising to me.
to implement it "correctly".
Shouldn't they have their destructors called when "~MyCSharpClass" isNo, finalizers (or call them destructors) don't call finalizers on instance
called?
members automatically (I don't know if and under which circumstances they do
in C++, but I guess you think they do in C#). As Göran has stated in the
reply below, objects that have finalizers are placed in the finalization
queue and GC calls their finalizers on its finalizing thread when it finds
appropriate.
While Göran has explained the "theory" of what you should do, please let me
sum up how to implement what you have in practice.
Your finalizer code (the code you've posted) should go to the Dispose(bool
disposing) method you'll add to your class. You will also implement
IDisposable interface and from its Dispose() method, you'll call
Dispose(true) and also call GC.SuppressFinalize(this); - which will prevent
the object to be placed in the finalization queue => the finalizer will not
be called when the object is collected. Your finalizer would call the
Dispose(bool) method and you have done so already, that's why you are
calling SuppressFinalize().
Your finalizer should call Dispose(false) which will get rid of all
references to its instance members (set them to null) and rely on let their
finalizers to be called in the next collection (in case they have any and
they have not been Disposed yet). You should not rely on the finalizer to be
called - it's there just for the case you "forget" to call
IDisposable.Dispose() method on your object.
The whole point you are missing, I think, is that IDisposable.Dispose() is
meant to be called deterministically by you when you know you need to clean
up the resources used by the object.
"Jeremy Chaney" <j*c@xxxxxxxxxxxxxx> wrote in message
news:e$T0EnvRHHA.4188@xxxxxxxxxxxxxxxxxxxxxxx
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
.
- References:
- Problem with C# not calling MC++ destrcutor
- From: Jeremy Chaney
- Re: Problem with C# not calling MC++ destrcutor
- From: Göran Andersson
- Re: Problem with C# not calling MC++ destrcutor
- From: Jeremy Chaney
- Re: Problem with C# not calling MC++ destrcutor
- From: Lebesgue
- Re: Problem with C# not calling MC++ destrcutor
- From: Jeremy Chaney
- Problem with C# not calling MC++ destrcutor
- Prev by Date: Re: How to develop cad like application
- Next by Date: Need help understanding how to call unmanaged dll
- Previous by thread: Re: Problem with C# not calling MC++ destrcutor
- Next by thread: Re: Problem with C# not calling MC++ destrcutor
- Index(es):
Relevant Pages
|