Re: Problem with C# not calling MC++ destrcutor
- From: Jeremy Chaney <j*c@xxxxxxxxxxxxxx>
- Date: Fri, 02 Feb 2007 10:09:18 -0800
I guess you are technically correct about managed objects not having a
destructor, but they do support "~MyClass". In .NET-land should I be
referring to that as a Finalizer instead of a destructor?
As far as cleaning up resources, is it acceptable to put my clean up
code in "~MyClass" but also make use of manually calling
myClass.Dispose()? There are cases where I know that I don't need
certain objects anymore and it would be nice if I could put my cleanup
code in one place and know that it would be called regardless of what
caused the object to be destroyed.
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?
Thanks,
--Jeremy
Göran Andersson wrote:
Jeremy Chaney wrote:.
I have an application written in C# that uses objects written in a
managed C++ DLL. When I exit my app, my C# classes have their
destructors called, but the MC++ objects that those classes hold
references to do not get invoked (I can observe this from both
breakpoints in the code, and trace output to the console).
I was under the impression that when my C# object goes out of scope, it
would automatically dispose of all of the references it is holding. I'm
new to GC, so maybe it doesn't work this way? I know that during
application execution objects with a ref count of 0 might not be cleaned
up right away, but when my process exits, shouldn't I be able to rely on
all of my objects being garbage collected?
Thanks,
--Jeremy
Managed classes doesn't have destructors, as there are no reference
counters at all, so nothing happens when the last reference goes away.
They may have Finalizers, that is instead called when the objects are
going to be garbage collected. You should avoid having a Finalizer on a
class if you don't need one, as having a Finalizer means that the object
has to go through at least two garbage collection cycles before it's
released.
If you need to release unmanaged resources in your classes, you should
implement the IDisposable interface (which includes have a Finalizer as
backup). If you only have managed resources in your classes, there is
nothing that you need to release, and so you don't need any Finalizer.
When the application ends, it will try to call the Finalizer of all
objects that has one, but if this takes too much time, it will just kill
the objects anyway. This is another reason to avoid having a Finalizer
if you don't need one, so that the finalizing queue only contains
objects that really needs to be finalized.
- Follow-Ups:
- Re: Problem with C# not calling MC++ destrcutor
- From: Göran Andersson
- Re: Problem with C# not calling MC++ destrcutor
- From: Lebesgue
- Re: Problem with C# not calling MC++ destrcutor
- References:
- Problem with C# not calling MC++ destrcutor
- From: Jeremy Chaney
- Re: Problem with C# not calling MC++ destrcutor
- From: Göran Andersson
- Problem with C# not calling MC++ destrcutor
- Prev by Date: Re: How to handle a property?
- Next by Date: Casting an enum, skips one for no reason?
- 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
|