More on deterministic finalization

  • Thread starter Thread starter Frank Rizzo
  • Start date Start date
F

Frank Rizzo

Ok, I need to get this straight in my head.
When I implement a destructor in my class, is it guaranteed to be called
when the class is destroyed or not? If not, when?

Also, does the framework call .Dispose in my class and if so, does it
call it when the class is destroyed. Or is it the responsibility of the
parent class to call .Dispose.

Thanks.
 
"If not, when?" ahh now theres the rub ... no-one knows. The destructor (or more acurately the finalizer) is called after the garbage collector realised there are no more active references to the object. So a prerequisite is that teh GC must run (when that is no-one knows - it depends on how often your application performs allocation and how much memory is on the machine, etc, etc). Now, when the GC realises you could be collected it suddenly says "damn, but it has a finalizer - well I'm not running that I have other things to do", so it puts the object on the finalization queue. Another thread runs asynchronously and will call your finalizer wen it gets to it in the queue - well probably, there are some situations where the finalizer won't run (some application shutdown scenarios).

The runtime kjnows absolutely nothing about Dispose its up to the consumers of your class to call it when they are funushed using you so you can clean up your resources at the soonest opportunity.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

Ok, I need to get this straight in my head.
When I implement a destructor in my class, is it guaranteed to be called
when the class is destroyed or not? If not, when?

Also, does the framework call .Dispose in my class and if so, does it
call it when the class is destroyed. Or is it the responsibility of the
parent class to call .Dispose.

Thanks.

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.languages.csharp]
 
Back
Top