Re: Problem with C# not calling MC++ destrcutor
- From: Göran Andersson <guffa@xxxxxxxxx>
- Date: Fri, 02 Feb 2007 18:30:18 +0100
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.
--
Göran Andersson
_____
http://www.guffa.com
.
- Follow-Ups:
- Re: Problem with C# not calling MC++ destrcutor
- From: Jeremy Chaney
- Re: Problem with C# not calling MC++ destrcutor
- References:
- Problem with C# not calling MC++ destrcutor
- From: Jeremy Chaney
- Problem with C# not calling MC++ destrcutor
- Prev by Date: Re: SQL Parameter
- Next by Date: Re: Need to calculate milleseconds until 1:00am...
- Previous by thread: Problem with C# not calling MC++ destrcutor
- Next by thread: Re: Problem with C# not calling MC++ destrcutor
- Index(es):
Relevant Pages
|
Loading