Re: Dispose must be thread-safe ?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 02/27/04


Date: Fri, 27 Feb 2004 10:46:52 -0000

Ales Pour <ales.pour@systinet.com> wrote:
> "Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
> > > 1.. Two threads are calling Dispose of the same object concurrently;
> >
> > Well yes - but that's surely a truism. However, one of those threads
> > *can* be the finalizer thread. If you intended that to be read as "Two
>
> I admit that due to my limited knowledge of GC-ing in .NET I was not able to
> track the whole discussion, but I dare to wonder - how come that one of
> threads can be the finalizer thread, while the object is still in use (since
> other thread still references it and even calls Dispose method on it)??

The other thread effectively doesn't need the reference any more - it's
(provably) not accessing any members any more. Basically, a lot of
people (me included) always assumed that any stack frame in an instance
method would include an implicit "root" (from the garbage collector's
point of view) to "this" - but it's *not* always a root, if it's not
needed.

Effectively, you could think of each method as having a local variable
called "this" which is passed to it. When that variable is no longer
being used, it doesn't stop the garbage collector from trying to
collect the object.

-- 
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Relevant Pages

  • Re: Tracking a memory leak.
    ... I think that the root of your misunderstanding (pardon the pun) is ... He correctly stated that the initial sweep doesn't start in the tree ... It then proceeds to follow all references ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Python language problem
    ... references and you cannot free an object. ... The garbage collector will do it for you automatically, ... access the resource, ... I create same object in Tree, I want to update Tree, when I need to ...
    (comp.lang.python)
  • Re: FastMM and garbage collection...
    ... It seems to me that the memory manager has all the information required ... Such references could be pretty much ... So how would your garbage collector determine which objects are no ... total control of all threads and have complete type information about ...
    (borland.public.delphi.language.basm)
  • Re: Setting objects to Nothing uses less memory!
    ... The garbage collector only does one pass and frees ... "bigObjects" and its internal instance of "lotsOfData" in the first ... > I think people often get confused about setting references to null. ... > The key is your InternalSetNull() method. ...
    (microsoft.public.dotnet.framework.performance)
  • Re: does python have useless destructors?
    ... "Called by the garbage collector on an object when garbage collection ... determines that there are no more references to the object." ... Let Java code ... > use Java idioms. ...
    (comp.lang.python)