On Tue, 22 Apr 2008 01:10:00 -0700, David Rosenkranz =
[quoted text, click to view] <DavidRosenkranz@discussions.microsoft.com> wrote:
> Pete, thank you for your detailed response. From your words I guess I =
=
[quoted text, click to view] > could
> not make myself perfectly clear.
I think you've been clear enough. But I'm not sure you fully understand=
=
memory management in .NET.
[quoted text, click to view] > Of course I know, that calling the Dispose method of an object does no=
t =
[quoted text, click to view] > mean
> killing references to this object. So, what am I doing:
>
> In the initialization method of one of my classes the following =
> assertion to
> a member variable (MyForm m_form) is made:
>
> m_myForm =3D new MyForm();
> m_myForm.Disposed +=3D new EventHandler(OnMyFormDisposed);
> m_myForm.Show();
>
> m_myForm is the only reference to the MyForm instance that _I_ am =
> creating
> and holding.
Not that it necessarily matters here, but it's not the only reference th=
at =
exists. The Application class, for example, maintains a list of open =
forms. As long as your form is open, it's referenced there as well.
[quoted text, click to view] > In OnMyFormDisposed I drop the reference
> m_myForm =3D null;
That's fine as far as it goes. But you should not overstate what that =
does.
[quoted text, click to view] > I assumed, that now the form was disposed (what else should be the =
> reason to
> fire the Disposed event)
The form is being disposed when your Disposed event handler is called, y=
es.
[quoted text, click to view] > and all of my selfmade references to the form were dropped.
You don't have control over all possible references. However, yes...I =
would expect generally that the instance will eventually be collected, =
assuming it's no longer reachable.
[quoted text, click to view] > I concluded that now the GC should have the possibility to clean up
> and free resources.
That's too vague. The disposal of the instance has already resulted in =
=
cleaning up and freeing of resources. All that remains is for the memor=
y =
to be collected. And assuming the instance is no longer reachable from =
=
_any_ data structure, yes...the collector has the possibility to collect=
=
that memory.
[quoted text, click to view] > From this point I profiled the memory consumption of my application an=
d =
[quoted text, click to view] > saw,
> that the GC never took care of MyForm instances. I looked at the GC =
> paths and
> realized that there was still at least one reference to the object. My=
> profiler says that "control of
> System.Windows.Forms.Control/ControlNativeWindow" is still pointing at=
my
> object.
What profiler are you using? Are you sure it's correct? If it is, note=
=
that having a reference to your form is a necessary, but not sufficient,=
=
condition to prevent collection.
[quoted text, click to view] > I assume that this has something to do with the controls which I
> place on the form: a ToolStrip with some ToolStripButtons and a =
> TabControl
> containing a ListView.
Why do you make that assumption? Unless something else is referring to =
=
those controls, those references would not prevent the collector from =
collecting the instance. Do you have something else referring to those =
=
controls? Have you ascertained for sure that those controls are in fact=
=
what's referencing the form instance?
[quoted text, click to view] > With trial-and-error I meanwhile discovered that the form is only garb=
age
> collected if I call this.Dispose() inside a Form.Closing event handler=
=
[quoted text, click to view] > AND I
> set the AllowItemReorder property of the contained ToolStrip to false.=
=
[quoted text, click to view] > Why
> does this work? Frankly, I don't know.
I don't know either. Assuming this is a modeless form we're talking =
about, I doubt that calling Dispose() has any effect at all (closing a =
modeless form causes it to be disposed). As long as the ToolStrip in th=
e =
form isn't being referred to anywhere else in your code, it having a =
reference to the form shouldn't have any effect.
I suppose there's a possibility that the AllowItemReorder property, when=
=
set to true, causes your ToolStrip item to be referenced by something =
else. But then presumably that would show up in your memory profiling.
Again: an instance being referenced does not in and of itself prevent th=
e =
instance from being collected. The object must be reachable. If you =
think your form instance isn't being collected, you need to follow the =
references all the way back to some "rooted" reference that causes the =
instance to be reachable.