Re: Dispose(bool), Idisposable, form closing etc.

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



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

.



Relevant Pages

  • Re: Local variable addresses changing
    ... > local variable adress changing problem after d=abs? ... The odd thing, though, is that your explanation precisely ... It's possible your assignments to local variables are ... overwriting stack locations that are used for other purposes. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Private variables question
    ... It struck me odd at first, but it saves me lots of grief. ... CodeHealer against all of my projects, ... local variables hide things. ...
    (borland.public.delphi.non-technical)
  • Re: Private variables question
    ... Bruce McGee wrote: ... It just looked odd. ... If it has a higher scope, it is most of the time a global one, or a ... I also have a few standard names for local variables: ...
    (borland.public.delphi.non-technical)
  • Re: Private variables question
    ... Rudy Velthuis [TeamB] wrote: ... local variables. ... It just looked odd. ...
    (borland.public.delphi.non-technical)
  • Re: ~Destructor equivalent in java?
    ... Assuming myObj was the last reference to the object, ... is only "eligible for garbage collection" and in general, ... allow local variables to go ...
    (comp.lang.java.programmer)