Hello,
When a control is disposed, should it first set its Tag property (if
present) to null ?
If the control will soon be eligible for garbage collection, then no.
If the control won't be eligible for garbage collection soon, then
setting the Tag property to null will allow the object references by
Tag to be collected sooner (assuming there are no other references to
that object).
I use a commercial control and I saw (with a .NET memory profiler) that
the objects I put in this property were not collected even when the
control is disposed (until I set the tag
property to null)
Calling Dispose has no effect on garbage collection. An object is
eligible for garbage collection when no live object has a reference it
regardless of whether or not Dispose has been called. Dispose simply
frees any resources that the object is holding. For UI controls
Dispose releases the associated Windows control. In no case does
Dispose cause the object to be garbage collected. If Dispose isn't
called explicitly, it will be called when the object is garbage
collected. Explicitly calling Dispose is simply a way to free
resources in a deterministic way, since the object may not be garbage
collected for a very long time.
If you aren't holding any references to the control (it is removed
from its container collection, any event handlers it added for other
objects have been removed, you don't have any variables that hold
reference to it, etc.) then it is eligible for garbage collection and
so is the object that its Tab property references.