Disposing controls

C

Carel Lotz

Hi

Our application dynamically assemblies screens at run-time
from definitions in a DB and we have been experiencing
memory leaks (more specific Handle leaks) to such a degree
that the application would eventually freeze on Win9x/ME
machines where the pool of resources is limited (unlike
Win2000/XP which will just keep on allocating resources).

The controls that we add to the form are all inherited
controls to provide a common interface to which to code
against. We eventually solved the leak by physically
calling dispose on each control when the form was closed.

We tested the same scenario with the same controls where
we beforehand added them to a static screen. When we
repeatedly displayed this form and closed the form no
memory leak occured. My question is, what is different
between having a form pre-polulated at design time with
controls and adding the same controls dynamically at run-
time? Why did we have to physically call Dispose in the
dynamic scenario and not in the static scenario?

Thanks
Carel
 
J

Jan Tielens

My guess is that the Dispose is called in the Designer Generated code when
using static controls. Maybe the dynamic controls are not added to the
components collection? Just an idea... so don't shoot me it I'm wrong! ;-)
 
Y

Ying-Shen Yu[MSFT]

Hi Carel,

Thanks for your post!

I'd like to know how you added these control to your form, and also which
method did you use to show your form, Show or ShowDialog ?

If you use Show method to display your form, the form will enumerat all
controls/components in it and call the Dispose(true) method. You may verify
this by
setting a breakpoint in the dispose method of your control.

If you use ShowDialog method, then things are a little different, because
We often write code as
If (form1.ShowDialog == DialogResult.True)
{
mytext = form1.textBox1.Text;
}
....
So the controls could not be disposed after the form closed. You need call
the Dispose method on the form explicitly to dispose the form and the
controls.

Does it helpful to your problem?
If you still have problem on it, please let me know more about your
problem,
give me some sode snippets or a repro sample to let me look into it, thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
G

Guest

Hi Ying-Shen

Thanks for the reply!

We add the controls to the form through calling the Add
on the form's Controls collection.

The way the forms in our application is structured is as
follows. We have a SDI interface consisting out of:
1) Main form containing the toolbar, menu items.
2) On this main form we have a process flow control on
the left hand side
3) To the right of the process control we add another
form called our canvas onto which we draw all our controls
This is the form that is(was) causing the problem.

On reading your reply, it was interesting to note that
the Dispose of the controls were related to what form of
Show/ShowDialog was called. When browsing through our
code I saw that we did not explicitly call a Show on our
canvas but only made it visible through setting the
Visible property on the form to True. Can it be that the
controls would only automatically be disposed if the Show
was explicitly called?

Carel
 
Y

Ying-Shen Yu[MSFT]

Hi Carel,

I saw this difference from Raghavendra Prabhu's blog, which is a member of
..NET client team,the original blog can be found here:
http://blogs.gotdotnet.com/rprabhu/permalink.aspx/af94ee11-5488-4ff0-bd5b-d7
b41a0d0ebf

Setting Visible to true is identical to the Show method. So Dispose method
should be called when you close the window, You may set a break point at
the dispose method of your control, and see if the method will be called
when the form is closed. If the Dispose method doesn't get called , maybe
you can give me a simple project to repro the problem.

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
P

Pete Davis

There is no such thing as "statically" adding controls in .NET. Unlike the
Windows SDK or VC++, .NET apps build forms at runtime (see the designer
generated code, that's exactly what it does), they're not created from
templates.

Are you keeping references to the controls anywhere?

I hate to say it, but it's likely something is being done wrong. Without
seeing the code, there's no way to be sure what it is, though..

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top