Hi,
I have an windows mdi application. I am creating a number of child windows.
Later, at some time i need to free up these child forms.
In the dispose method of the child forms, do I explicitly dispose the
controls placed on the child form ??

What are the general rules. I went through MSDN but nowhere was it
explicitly mentioned regarding windows forms.

TIA.

Re: Disposing Child Forms by Eric

Eric
Wed Apr 21 10:18:15 CDT 2004

It's is my experience that you should call Dispose on anything and
everything that implements IDisposable.

Here's a sample function that I wrote that will call dispose on all
non-public instance variables within a class. Call this from within the
Form's Dispose method. This prevents you from having to explicitly list all
the calls inside Dispose.

private void Disposer()
{
Type type = this.GetType();
if (type != null)
{
foreach(FieldInfo fi in type.GetFields(BindingFlags.NonPublic |
BindingFlags.Instance))
{
object field = fi.GetValue(this);
if (field is IDisposable)
{
((IDisposable)field).Dispose();
Console.WriteLine("disposed field:" + fi.Name);
}
}
}
}

HTH;
Eric Cadwell
http://www.origincontrols.com




Re: Disposing Child Forms by Alok

Alok
Wed Apr 21 22:55:25 CDT 2004

Hi Eric,
The piece of code really helped.

But what about second level controls. For instance, if i have a user
control, would i have to write this code in it's dispose method as well ??

TIA

"Eric Cadwell" <ecadwell@ns.insight.com> wrote in message
news:uwfrfP7JEHA.1392@TK2MSFTNGP09.phx.gbl...
> It's is my experience that you should call Dispose on anything and
> everything that implements IDisposable.
>
> Here's a sample function that I wrote that will call dispose on all
> non-public instance variables within a class. Call this from within the
> Form's Dispose method. This prevents you from having to explicitly list
all
> the calls inside Dispose.
>
> private void Disposer()
> {
> Type type = this.GetType();
> if (type != null)
> {
> foreach(FieldInfo fi in type.GetFields(BindingFlags.NonPublic |
> BindingFlags.Instance))
> {
> object field = fi.GetValue(this);
> if (field is IDisposable)
> {
> ((IDisposable)field).Dispose();
> Console.WriteLine("disposed field:" + fi.Name);
> }
> }
> }
> }
>
> HTH;
> Eric Cadwell
> http://www.origincontrols.com
>
>
>



Re: Disposing Child Forms by Eric

Eric
Wed Apr 21 10:31:50 CDT 2004

Yes, everywhere. There are certain circumstances in which a form may not be
disposed. In that event, calling Dispose on all objects allows unmanaged
resources to be reclaimed. This may protect you from OutOfMemoryExceptions.
Trust me, I have seen it happen.

The biggest offender? Images.

There are hotfixes out for the "leaks", but I don't think they are publicly
available for 1.0 or 1.1. I could be wrong.

-Eric