Re: Dispose(bool), Idisposable, form closing etc.
- From: Peter Duniho <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Wed, 21 Nov 2007 00:16:03 -0800
On 2007-11-20 23:32:39 -0800, Peter Duniho <NpOeStPeAdM@xxxxxxxxxxxxxxxx> said:
[...]
I wasn't talking about Application.Run at all. I was just using a regular
function that I defined myself and passed in an instance of a class I
defined. The only way I could get the class to be GCd before the function
exits was to set the parameter to null inside the function.
It is well-known and documented that a reference held only by a variable that is no longer used is eligible for garbage collection even while the variable is in scope. It's why GC.KeepAlive() even exists.
If you were unable to reproduce that behavior, it only means your test was flawed.
Okay, my apologies on this point.
I still say that unless you look at Application.Run(), you can't really say for sure what it's doing. It could in fact set the parameter to null after adding the reference to the list of open forms (by showing the form). Why it would, I don't know...seems like an odd thing to do, but then I've seen some seemingly odd code in Windows (though usually it was only "odd" because I didn't understand the need for it).
However, you're right that parameters are treated differently from local variables, and as long as the parameter references the instance for the duration of the call to the method, the instance won't be collected.
I'm (obviously) a little surprised that parameters aren't treated the same as local variables. I wonder if there's some subtle difference in the behavior of a parameter I'm unaware of. At least for parameters passed by value, I generally view them just like local variables except that the caller gets to initialize them. But here's one example where they definitely don't behave like a local variable.
Pete
.
- Follow-Ups:
- Re: Dispose(bool), Idisposable, form closing etc.
- From: Brian Gideon
- Re: Dispose(bool), Idisposable, form closing etc.
- References:
- Dispose(bool), Idisposable, form closing etc.
- From: rbrowning1958
- Re: Dispose(bool), Idisposable, form closing etc.
- From: Nicholas Paldino [.NET/C# MVP]
- Re: Dispose(bool), Idisposable, form closing etc.
- From: rbrowning1958
- Re: Dispose(bool), Idisposable, form closing etc.
- From: Michael C
- Re: Dispose(bool), Idisposable, form closing etc.
- From: Peter Duniho
- Re: Dispose(bool), Idisposable, form closing etc.
- From: Michael C
- Re: Dispose(bool), Idisposable, form closing etc.
- From: Peter Duniho
- Re: Dispose(bool), Idisposable, form closing etc.
- From: Michael C
- Re: Dispose(bool), Idisposable, form closing etc.
- From: Peter Duniho
- Re: Dispose(bool), Idisposable, form closing etc.
- From: Michael C
- Re: Dispose(bool), Idisposable, form closing etc.
- From: Peter Duniho
- Dispose(bool), Idisposable, form closing etc.
- Prev by Date: Re: Bug in 2008
- Next by Date: Re: best method for common functions
- Previous by thread: Re: Dispose(bool), Idisposable, form closing etc.
- Next by thread: Re: Dispose(bool), Idisposable, form closing etc.
- Index(es):
Relevant Pages
|