There is another case (though an edge case) where Finalizaers aren't called.
The case is where during tear-down of a process, if during calling of
Finalizers is increasing memory usage (usually if someone is doing something
bad), the GC will abort the Finalizations and just kill the whole heap.

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Trey Nash
Sent: Friday, September 19, 2008 1:55 PM
To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
Subject: Re: [ADVANCED-DOTNET] Unmanaged resources in a static class

Hi Simon,

The answer to your questions is "Yes and No".  :-)  I'll explain both below:

Yes:  Finalizers will be called when the application domain is shutting down
and/or when the CLR is shutting down.  You can determine if this is the case
by referencing the Environment.HasShutdownStarted property.  Although it is
unwise to reference other objects in one's finalizer, in some cases folks
attempt to access static objects in their finalizer assuming that, since
they are static, they will be available.  However, instead of making such a
bold assumption, those folks should gate their access to those static
objects based on the return of the HasShutdownStarted property.  Because
when that guy returns true, those static objects may already have been
finalized.

No:  Finalizers may not ever be called on all objects if your application
runs as a system service and the system is shutting down!!!!  This depends
on various factors, including the version of the OS.  For example, at the
native level, services receive the SERVICE_CONTROL_SHUTDOWN notification.
However, on 2000/XP/2003, for example, that notification is of limited use
because you only have about 20 seconds to get any useful work done.  When
that 20 seconds times out, your service process is *terminated* rudely.  So,
you can imagine that if you have a managed app running as a service and the
finalizers need to do a lot of clean-up during process shutdown, the work
may not complete before the service is terminated.  Now, that said,
Microsoft has introduced a new native service notification starting with
Vista called SERVICE_CONTROL_PRESHUTDOWN.  As you can imagine, this notifies
the service that the system is about to shut down and gives the service as
much time as it needs to clean up.  However, I don't know if the Microsoft
CLR implementations make use of this new notification on the Vista and
Windows 2008 platforms.

Hope this helps,

        -Trey


---------------------------------------
Trey Nash

Check out my book "Accelerated C# 2008"
http://www.amazon.com/Accelerated-C-2008-Trey-Nash/dp/1590598733



> -----Original Message-----
> From: Discussion of advanced .NET topics. [mailto:ADVANCED-
> [EMAIL PROTECTED] On Behalf Of Simon Robinson
> Sent: Friday, September 19, 2008 6:55 AM
> To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
> Subject: Re: [ADVANCED-DOTNET] Unmanaged resources in a static class
>
> Thanks for the replies everyone.  Static reference to an instance does
> sound on the surface to be the easiest solution. I have one doubt
> though:
> It's occurred to me to wonder whether finalizers are guaranteed to be
> executed when the app exits? They are executed when a garbage
> collection
> happens but that's not the same thing. Does .NET do a garbage
> collection
> on app exit or does it just rely on Windows freeing up everything?  If
> it
> doesn't then keeping a static reference presumably won't make any
> difference.
>
> Ta
>
> Simon
>
> On Wed, 17 Sep 2008 19:33:14 -0700, Greg Young
> <[EMAIL PROTECTED]>
> wrote:
>
> >why not keep a static reference to an instance of the class instead of
> >using a static class?
> >
> >Cheers,
> >
> >Greg
>
> ===================================
> This list is hosted by DevelopMentorR  http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com

===================================
This list is hosted by DevelopMentor.  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to