Re: C# equivalent to a smart pointer???



Peter Duniho <NpOeStPeAdM@xxxxxxxxxxxxxxxx> wrote:
I would like to add a couple of comments to the concept of references here,
because I personally find them non-intuitive, and in fact only came across
the issues by accident. IMHO, they're especially important to someone
coming at this from a C background (as many of us do), because the behavior
deviates from what we've learned to expect. Hopefully this will make some
good reading for you once you're back at work on Monday. :)

Both issues relate to (overly, IMHO) aggressive optimizations of local
variables on the part of the C# compiler. One of the issues, I learned
about in this newsgroup. The basic idea is that the compiler may detect
that a local variable appears to be an alias for some other variable, and
optimize out the use of the local altogether. For example:

{
Object objLocal = objGlobal;

if (objLocal != null)
{
objLocal.DoSomething();
}
}

In this example, you might naively think that because you've stored the
reference locally, that even if the objGlobal reference is set to null,
you're okay. Apparently that's an incorrect assumption in some cases, and
if it's possible for the objGlobal variable to get set to null after the
if() statement (say, by a different thread), the line that actually calls
the DoSomething() method could wind up resolving to
"objGlobal.DoSomething()" and cause a null-reference exception when it
executes.

I should point out that this is only my understanding based on an MS
blog entry. At some point I hope to correspond with the author to check
that that really *is* the case.

The second issue I came across reading documentation on an entirely
different issue. It showed up in some sample code in the documentation.
The issue is that the lifetime of a local variable is NOT determined by
scope. The compiler will treat the object essentially as out-of-scope even
within the same scope if it's not used in any code past a certain point.

This one I think is more reasonable - it's really just a case of
getting out of a C++ idiom which doesn't apply in .NET. Note that it's
not C# which is doing the work here - it's the JIT. Basically, one
shouldn't be using scope to determine the lifetime of an object.

I should say, however, that the case you've given isn't the strangest
one at all (although I usually see questions relating to it with Mutex
instead of Timer). What's weirder is when the finalizer for an object
can be invoked *while a method is still running in that object*.

I've had a go at hooking up an example to show it, and I can't at the
minute, but the main thing is that the GC is free to collect an object
when it knows that no thread is going to access the member variables of
the object again. Just to scare you a bit more :)

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



Relevant Pages

  • Re: How does "new" work in a loop?
    ... compiler can detect the variable's lifetime, ... out the whole lexical scope. ... So you mean that the scope of a variable only last one single iteration ... The variable's lifetime may be smaller ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Lock-free reference counting
    ... The scope of t is the entire function foo, ... The reference's lifetime is considerably shorter than ... The only way for it to collect it is if no reference is ... Bindings are introduced by various language constructs. ...
    (comp.programming)
  • Re: Lock-free reference counting
    ... If you mean that he's very confused about the difference between scope ... they destroy objects immediately their lifetime ends, ... variable binding whereas tracing collectors don't do this. ... tracing collectors can reclaim dead space on the stack whereas ...
    (comp.programming)
  • Re: VLA and goto -- diagnostic required?
    ... I'm currently working on updating an older compiler up to C99. ... however, just jumps over the actual VLA declaration, within the ... "A goto statement shall not jump from outside the scope of an ...
    (comp.lang.c)
  • Re: How long does recordset stay open?
    ... storing preferences is to keep them in a text file and load them from ... You can give it wider scope and a longer lifetime if you declare it in the ... recordset with various default variables used by my program. ...
    (comp.databases.ms-access)

Loading