Re: C# equivalent to a smart pointer???
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Sat, 7 Oct 2006 21:42:47 +0100
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
.
- Follow-Ups:
- Re: C# equivalent to a smart pointer???
- From: Peter Duniho
- Re: C# equivalent to a smart pointer???
- References:
- C# equivalent to a smart pointer???
- From: Ian
- Re: C# equivalent to a smart pointer???
- From: Peter Duniho
- Re: C# equivalent to a smart pointer???
- From: Ian
- Re: C# equivalent to a smart pointer???
- From: Peter Duniho
- C# equivalent to a smart pointer???
- Prev by Date: Re: Why instantiate a class, all of whose methods, properties, and events are static?
- Next by Date: Re: How to assign a reference to a class member to another class member?
- Previous by thread: Re: C# equivalent to a smart pointer???
- Next by thread: Re: C# equivalent to a smart pointer???
- Index(es):
Relevant Pages
|
Loading