PC Review


Reply
Thread Tools Rate Thread

Disposing controls

 
 
Carel Lotz
Guest
Posts: n/a
 
      8th Dec 2003
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
 
Reply With Quote
 
 
 
 
Jan Tielens
Guest
Posts: n/a
 
      8th Dec 2003
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! ;-)

--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan
"Carel Lotz" <(E-Mail Removed)> schreef in bericht
news:05f201c3bd7f$008db8d0$(E-Mail Removed)...
> 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



 
Reply With Quote
 
Ying-Shen Yu[MSFT]
Guest
Posts: n/a
 
      9th Dec 2003
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.

 
Reply With Quote
 
Guest
Posts: n/a
 
      9th Dec 2003
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


>-----Original Message-----
>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.
>
>.
>

 
Reply With Quote
 
Ying-Shen Yu[MSFT]
Guest
Posts: n/a
 
      10th Dec 2003
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/p...8-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.

 
Reply With Quote
 
Pete Davis
Guest
Posts: n/a
 
      10th Dec 2003
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

"Jan Tielens" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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! ;-)
>
> --
> Greetz,
> Jan
> __________________________________
> Read my weblog: http://weblogs.asp.net/jan
> "Carel Lotz" <(E-Mail Removed)> schreef in bericht
> news:05f201c3bd7f$008db8d0$(E-Mail Removed)...
> > 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

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Disposing Rob Microsoft ASP .NET 4 11th Jan 2007 01:47 PM
Disposing - when? TyBreaker Microsoft VB .NET 1 22nd Aug 2006 12:01 PM
hosting controls in IE, best practice for disposing of memory? Kristopher Wragg Microsoft Dot NET Framework 0 6th Jun 2006 02:28 PM
Disposing controls jnospamster@gmail.com Microsoft Dot NET Framework Forms 1 3rd Mar 2006 05:14 AM
disposing controls on panel David Microsoft C# .NET 9 30th Nov 2004 07:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:56 PM.