Re: About speed



"Ingvar Nilsen" <no.spam@xxxxxxxxxxxxx> wrote:

Barry Kelly wrote:

protected virtual void Dispose(bool disposing)

In what case would this be called with disposing = false?

So the short answer is never, unless a descendant implements a
finalizer.

"disposing" is true iff Dispose(bool) was called explicitly via
Dispose(), Close(), IDisposable.Dispose() or some other explicit
resource disposal method. An explicit resource disposal means one can
safely dispose one's owned resources.

"disposing" is false iff it was called by overriding the Finalize()
method (the way you override Finalize() in C# is by implementing ~T()
for a type T). Being called by the finalizer implies being called on the
finalizer thread after being collected by the GC, so accessing other
managed classes may not work - they might be finalized already, they
might not, or the field might be nulled out - basically, don't do it.
I'm not sure exactly what happens because I've never done it myself :)

The pattern I posted, which consists of IDisposable and the protected
virtual void Dispose(bool) alone, doesn't implement a finalizer, so it
won't get called with disposing == false *unless* a descendant class,
which does encapsulate an unmanaged resource directly, implements a
finalizer.

I'd like to point anyone who's got more interest in the topic to:

http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=88e62cdf-5919-4ac7-bc33-20c06ae539ae

-- Barry

--
http://barrkel.blogspot.com/
.



Relevant Pages

  • Re: StatusStrip control leaks its collection items
    ... you'd followed the .NET requirement of disposing disposable objects, ... But I still maintain that an object that wraps an unmanaged resource ... must release that resource in its finalizer. ... Now back to the problem at hand, I am finding that controls removed ...
    (microsoft.public.dotnet.framework)
  • Re: About speed
    ... IDisposable needs a finalizer, and most code that I see that implements ... void IDisposable.Dispose ... protected virtual void Dispose(bool disposing) ... then disposed explicitly (using IDispose), ...
    (borland.public.delphi.non-technical)
  • Re: Can lock into a dispose?
    ... And Dispose of components shouldn't be called by the Finalizer ... wich has to occur during disposing aswell as during ... But either disposing is true or false, I would serialize the class, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Accessing private data during finalize/dispose
    ... > I understand the basics of finalization and implementing IDisposable and how one is guaranteed access to managed objects only when working through the IDisposable interface. ... class A provides management for the creation ... > protected Dispose(bool disposing) ... You can safely access other objects from a finalizer, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Continuations in Common Lisp (with apologies)
    ... without explicit locks.) ... dictionaries the object was present in. ... A finalizer can remove them ... The weak reference itself doesn't keep its components alive. ...
    (comp.lang.lisp)