You should not be doing things like logging when the finalizer is running. It is entirely possible that the logger your code is trying to call (or the objects it uses) has been garbage collected by the time your finalizer is called, or at the very least it has been marked for GC.

This is from the link you sent...
"If disposing equals false, the method has been called by the runtime from inside the finalizer and only unmanaged resources can be disposed. When an object is executing its finalization code, it should not reference other objects, because finalizers do not execute in any particular order. If an executing finalizer references another object that has already been finalized, the executing finalizer will fail."

So the short answer is don't try to log when called from the finalizer because the logger is in an unknown state.

aaron

Michael Hinkel wrote:
All,
 
I'd like to log some information in a
 
  protected virtual void Dispose(bool disposing)
 
method which can be called either by Dispose() or by the finalizer
(implemented according to Microsoft's proposal "Implementing a Dispose
Method"; see
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconimpleme
ntingdisposemethod.htm).

If Dispose(bool disposing) is called by Dispose() everything works fine,
but if the method is calld by the finalizer on application shutdown, no
log entry is created. If I use a Console.WriteLine(message) the message
is sent to the Console even on application shutdown.

Any suggestions?


Michael 

_______________________________________
DATATRAK Deutschland GmbH
Michael Hinkel
Software Developer
Rochusstrasse 65
53123 Bonn

http://www.datatrak.net
  


  

Reply via email to