Google Grupper understøtter ikke længere nye Usenet-opslag eller -abonnementer. Tidligere indhold er fortsat synligt.

Memory Leak in Form

10 visninger
Gå til det første ulæste opslag

bdoyle

ulæst,
23. jan. 2004, 14.06.0923.01.2004
til
I seem to be having memory leak problems. If you open a form using Form.ShowDialog() within anther form,
the framework doesnt release all of the memory used by the new form when the form closes? Whats going on here

Chris Tacke, eMVP

ulæst,
23. jan. 2004, 14.16.0823.01.2004
til
Since this is a memory managed environment, you should not expect the memory
to be recovered immediately after closing the Form. If you're not actually
running into a low/out of memory situation you shouldn't be concerned with
what the GC does and when.

Of course that assumes that the Form is properly closed and is properly
disposing it's resources. After you close it, it's not a bad idea to
explicitly call Dispose() on it.

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net


"bdoyle" <bdoy...@carenttech.com> wrote in message
news:14767AA3-7CA3-4600...@microsoft.com...

Alex Feinman [MVP]

ulæst,
23. jan. 2004, 17.38.2223.01.2004
til
From my earlier experience (pre-RTM, and definitely pre-service pack) you
must call Form.Dispose() because there was an internal reference (staic
Control.m_rlControls) held indefinitely, so the GC would never kick in. I
believe this has been resolved in SP2 (but haven't had a chance to make sure
of that)

"Chris Tacke, eMVP" <cta...@spamfree-opennetcf.org> wrote in message
news:uAbfcVe4...@TK2MSFTNGP10.phx.gbl...

Katie Schaeffer [MSFT]

ulæst,
23. jan. 2004, 19.21.3123.01.2004
til
Hi,

Keep in mind, that when you show a form via .ShowDialog, it is not disposed
automatically when closed (ByDesign). This is because modal dialogs are
intended to be reusable.

-Katie

This posting is provided "AS IS" with no warranties, and confers no rights.

***.Net Compact Framework Info***
Faq:
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.a
spx
QuickStarts: http://samples.gotdotnet.com/quickstart/CompactFramework/
Samples:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/h
tml/CompactfxTechArt.asp

--------------------
| Thread-Topic: Memory Leak in Form
| thread-index: AcPh4/WoPJJBgLV4TP68FOfoitSE4w==
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
| From: =?Utf-8?B?YmRveWxl?= <bdoy...@carenttech.com>
| Subject: Memory Leak in Form
| Date: Fri, 23 Jan 2004 11:06:09 -0800
| Lines: 2
| Message-ID: <14767AA3-7CA3-4600...@microsoft.com>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| Path: cpmsftngxa07.phx.gbl
| Xref: cpmsftngxa07.phx.gbl
microsoft.public.dotnet.framework.compactframework:43886
| NNTP-Posting-Host: tk2msftcmty1.phx.gbl 10.40.1.180
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

- robins @ntlworlddotcom Martin Robins

ulæst,
24. jan. 2004, 06.11.2624.01.2004
til
Wrong group I know, but does this apply to full framework also? If so, I
missed that!

"Katie Schaeffer [MSFT]" <kat...@online.microsoft.com> wrote in message
news:KmDKLAh4...@cpmsftngxa07.phx.gbl...

Tim Wilson

ulæst,
24. jan. 2004, 09.31.4624.01.2004
til
Yes, this applies to the desktop .Net Fx too. From the help for
Form.ShowDialog(...):

When a form is displayed as a modal dialog box, clicking the close form
button (the button with an "X" at the top right of the form) causes the form
to be hidden and the DialogResult property to be set to DialogResult.Cancel.
Unlike modeless forms, the Close method is not called by the .NET Framework
when the user clicks the close form button of a dialog box or sets the value
of the DialogResult property. Instead the form is hidden and can be shown
again without creating a new instance of the dialog box. Because a form
displayed as a dialog box is not closed, you must call the Dispose method of
the form when the form is no longer needed by your application.

--
Tim Wilson
.Net Compact Framework MVP

"Martin Robins" <martin - robins @ ntlworld dot com> wrote in message
news:%236DnQrm...@tk2msftngp13.phx.gbl...

BDOYLE

ulæst,
26. jan. 2004, 13.41.0626.01.2004
til
Katie Schaeffer,
Thanks for your reply on the MSDN Newsgroup about showdialog() not releasing memory when it closes. What is the best way to show multiple forms? Do you have any example of an application flow, what are good practices? Do you always start with Application.Run(mainform) ? Then each form from within it do a show or showdialog, if you do a show, how do you handle when it closes and refresh the form prior? If you do showdialog, do you have to follow with dispose, can you then create the form new again and showdialog again? Do you need some kind of message pump around the whole thing? Our program goes about 6 forms deep using showdialogs, but the last two get closed and reopened multiple times, and we get alot out of memory problems. Any help or examples on 'best practices' would be appreciated? Ive looked everywhere for last 3 months.

Please, please, please with sugar on top!


----- Katie Schaeffer [MSFT] wrote: -----

Katie Schaeffer [MSFT]

ulæst,
26. jan. 2004, 22.09.2526.01.2004
til
Hi,

Ok - so I'll start with some of the differences between using .Show and
using .ShowDialog:

.ShowDialog - This gives you a modal dialog, which means that the
previously topmost form is disabled until this dialog is dismissed.
Dialogs are also meant to be re-usable, allowing you to call .ShowDialog
multiple times after the dialog has been closed (either via the user
clicking the 'ok' button on the top of your ppc, or programmitically by
calling Close() or setting the DialogResult property). This is typically
used when information is to be collected or displayed to the user, and the
user needs to provide or acknowledge that information before interacting
with the application further. Also, a call to .ShowDialog will not return
until the dialog shown is closed.

.Show - This simply makes a form visible. It doesn't disable the previous
form, and you need to do extra work to prevent the form from being disposed
upon closing (such as set CancelEventArgs.Cancel = True in the closing
event and hide the form). Code after the call to .Show will execute
immediately (without waiting for the form you just displayed to be closed).

As far as best practices for application flow, you can pretty much make any
combination of .Show and .ShowDialog work, but here are my thoughts about
what is easiest. Applications that have a very linear path (Form A -> Form
B -> Form C -> Form B -> Form D -> Form B -> Form A) would use .ShowDialog.
It is expected that only one form is being interacted with at a time, and
when you close a form you tend to display the previous form. Applications
that are navigated more freely (Form A -> Form B -> Form C -> Form A) may
have an easier time using .Show. Notice that in this case after Form C is
closed, you want to go back to the main form.

If you're running into memory problems - you can ensure that only forms
currently visible are kept around. When using .ShowDialog, you can add a
this.Dispose call to your Closed event. When using .Show, this is already
done for you. (If the form has been disposed, you'll have to create a new
instance before showing it again - myForm2 = new Form2(); myForm2.Show.
You'll know right away if you forgot to do this, since you'll get an
ObjectDisposedException). Of course, re-creating forms unnecessarily will
have performance implications, so I'd recommend re-using forms whenever
possible. If you're using graphics and bitmap objects - be sure to Dispose
those, as they can eat up quite a bit of memory.

As far as message pumps - the typical way to start an application is to use
Application.Run. This designates a main form and sets up a message pump.
When this main form is closed, that message pump will also shut down. A
call to .ShowDialog also sets up a message pump (that shuts down when the
dialog is closed). All the message pump does is loop continuously
processing events, until the respective form is closed. This means code
placed directly after the call to .ShowDialog, or Application.Run doesn't
get executed until the respective form is closed.

Ok - I think that answers most of your questions (and hopefully I don't
have too many typos). My advice is to start up a simple application with a
few forms, and play around with this stuff. Also, the links below are good
sources of information. Good Luck :-).

-Katie

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| Thread-Topic: Memory Leak in Form

| thread-index: AcPkO/U44/9rUyBmQRybdbk+vTYC+w==
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
| From: =?Utf-8?B?QkRPWUxF?= <bdoy...@carenttech.com>
| References: <14767AA3-7CA3-4600...@microsoft.com>
<KmDKLAh4...@cpmsftngxa07.phx.gbl>
| Subject: RE: Memory Leak in Form
| Date: Mon, 26 Jan 2004 10:41:06 -0800
| Lines: 60
| Message-ID: <149EF5E1-B997-424E...@microsoft.com>


| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| Path: cpmsftngxa07.phx.gbl
| Xref: cpmsftngxa07.phx.gbl

microsoft.public.dotnet.framework.compactframework:44050

bdoyle

ulæst,
2. feb. 2004, 15.41.0902.02.2004
til
Thanks katie for the help, maybe im still missing something... If you are resusing a form (i.e. i have a controller (cache) that keeps the forms and when they need to be shown, it will resuse or create if null) im getting this weird reaction on my pocketpc
if you have 2 forms set to FormWindowState.Maximized, show one after the other, then show first again, the second time it shows it moves the
form window to location 0,26 then if you do it again its fine, but this only happens on the second refresh? the 3,4,5... are fine after that? If you do a showdialog, it does the same...?

try this code:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
public class TestForm3 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox tb;

public TestForm3(string t)
{
InitializeComponent();
tb.Text = t;
}

private void InitializeComponent()
{
this.tb = new System.Windows.Forms.TextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.tb.Text = "textBox1";
this.textBox1.Location = new System.Drawing.Point(104, 0);
this.textBox1.Text = "Top";
this.Controls.Add(this.textBox1);
this.Controls.Add(this.tb);
this.Load += new System.EventHandler(this.TestForm3_Load);
}
private void TestForm3_Load(object sender, System.EventArgs e)
{
this.MaximizeBox = false;
this.MinimizeBox = false;
this.ControlBox = false;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
this.Menu = null;
}
static void Main()
{
TestForm3 t = new TestForm3("1");
TestForm3 t2 = new TestForm3("2");

//first time
t.Show();
t.Refresh();
t2.Show();
t2.Refresh();
//second time location moved
t.Show();
t.Refresh();
t2.Show();
t2.Refresh();
t2.Hide();
System.Threading.Thread.Sleep(10000);
}
}

What gives?

Katie Schaeffer [MSFT]

ulæst,
3. feb. 2004, 23.08.2703.02.2004
til
Looks like you've come across a bug with fullscreen forms. We've fixed a
number of issues around this - but I'll check our current builds and see
your code still repros.

Meanwhile, if you're looking for a workaround - you can try removing the
line 'this.FormBorderStyle = FormBorderStyle.None'. This makes your form
act as a popup form (also allowing it to be resized to not take up the
entire screen). The PocketPC OS has some known issues with it's handling
of WS_POPUP forms.

Hope this helps,
-Katie

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| Thread-Topic: Memory Leak in Form

| thread-index: AcPpzONTZJQ4VMJjS+alT8lTG+DfZg==
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
| From: =?Utf-8?B?YmRveWxl?= <bdoyle80...@carenttech.com>
| References: <14767AA3-7CA3-4600...@microsoft.com>
<KmDKLAh4...@cpmsftngxa07.phx.gbl>
<149EF5E1-B997-424E...@microsoft.com>
<FY3PKMI...@cpmsftngxa07.phx.gbl>


| Subject: RE: Memory Leak in Form

| Date: Mon, 2 Feb 2004 12:41:09 -0800
| Lines: 64
| Message-ID: <7DD402F1-EAF1-4AE3...@microsoft.com>


| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| Path: cpmsftngxa07.phx.gbl
| Xref: cpmsftngxa07.phx.gbl

microsoft.public.dotnet.framework.compactframework:44654


| NNTP-Posting-Host: tk2msftcmty1.phx.gbl 10.40.1.180
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|

- robins @ntlworlddotcom Martin Robins

ulæst,
5. feb. 2004, 16.28.5905.02.2004
til
Katie,

I have tonight been running some examples and cannot necessarily reproduce
what you are saying.

I have a CF application with 2 forms. The first form has a timer that, on
the tick event, creates a new instance of the second form and displays it
using ShowDialog(). The second form also has a timer that then sets
DialogResult to DialogResult.OK in order to close that form. When I run the
application, the memory is used up constantly as you (and the original
writer) describe. If I add a form.Dispose() after the form.ShowDialog(), the
memory usage does not increase, also as described.

If I replace the this.DialogResult = DialogResult.OK with this.Close(), and
again disable the form.Dispose() after the form.ShowDialog(), the memory
usage remains steady with no noticable increase in the memory being used.
According to your message, the results should be the same for both
scenarios.

Incidentally, I am running CF SP2 for these tests in case that makes a
difference.

Regards,

Martin.


"Katie Schaeffer [MSFT]" <kat...@online.microsoft.com> wrote in message

news:FY3PKMI...@cpmsftngxa07.phx.gbl...


> Hi,
>
> Ok - so I'll start with some of the differences between using .Show and
> using .ShowDialog:
>

> ShowDialog - This gives you a modal dialog, which means that the
> previously topmost form is disabled until this dialog is dismissed.
> Dialogs are also meant to be re-usable, allowing you to call .ShowDialog
> multiple times after the dialog has been closed (either via the user
> clicking the 'ok' button on the top of your ppc, or programmitically by
> calling Close() or setting the DialogResult property). This is typically
> used when information is to be collected or displayed to the user, and the
> user needs to provide or acknowledge that information before interacting
> with the application further. Also, a call to .ShowDialog will not return
> until the dialog shown is closed.
>

Katie Schaeffer [MSFT]

ulæst,
6. feb. 2004, 15.47.4906.02.2004
til
Hi Martin,

What you are saying makes sense. If you use .ShowDialog, the form will not
automatically be disposed upon closing (either via setting the DialogResult
or via Close()). If you re-use the same form instance and call .ShowDialog
multiple times, you shouldn't see the memory usage increase. If, however,
you create a new instance, then it's expected that the memory usage will
increase.
Am I missing something?

-Katie

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| From: "Martin Robins" <martin - robins @ ntlworld dot com>


| References: <14767AA3-7CA3-4600...@microsoft.com>
<KmDKLAh4...@cpmsftngxa07.phx.gbl>
<149EF5E1-B997-424E...@microsoft.com>
<FY3PKMI...@cpmsftngxa07.phx.gbl>

| Subject: Re: Memory Leak in Form
| Date: Thu, 5 Feb 2004 21:28:59 -0000
| Lines: 224
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <e1ZfO8C7...@TK2MSFTNGP10.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| NNTP-Posting-Host: cpc3-shep1-5-0-cust37.nott.cable.ntl.com 213.105.123.37
| Path:
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.
phx.gbl
| Xref: cpmsftngxa07.phx.gbl
microsoft.public.dotnet.framework.compactframework:45040
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

- robins @ntlworlddotcom Martin Robins

ulæst,
7. feb. 2004, 08.17.3007.02.2004
til
Yes Katie, you have misunderstood my tests.

For the test I am creating a new instance of the form every time. Using
DialogResult causes the memory usage to increase dramaticcally, using
Close() causes no memory use increase at all.

I am not manually disposing the form at all during the tests.

Martin.


"Katie Schaeffer [MSFT]" <kat...@online.microsoft.com> wrote in message

news:a9m28JP7...@cpmsftngxa07.phx.gbl...

Alex Feinman [MVP]

ulæst,
7. feb. 2004, 21.09.2607.02.2004
til
When you are setting DialogResult, the ShowDialog method returns
immediately. The form is not destroyed and can be reused quickly by calling
ShowDialog again. Calling Close forces form to release all resources and
memory. This is by design. If you use DialogResult (which is the right thing
to do), you must call Dispose when your form goes out of scope.

"Martin Robins" <martin - robins @ ntlworld dot com> wrote in message
news:eZmC6yX7...@TK2MSFTNGP10.phx.gbl...

- robins @ntlworlddotcom Martin Robins

ulæst,
8. feb. 2004, 07.03.5808.02.2004
til
This is what I found, but it contradicts what Katie has said above. This is
why I was asking for clarification.

"Alex Feinman [MVP]" <publi...@alexfeinman.com> wrote in message
news:%23dHAVie...@TK2MSFTNGP10.phx.gbl...

Katie Schaeffer [MSFT]

ulæst,
13. feb. 2004, 02.36.1813.02.2004
til
Hi Martin,

So for both your tests you are using .ShowDialog, creating a new form
instances, and never calling .Dispose()? You should be seeing your memory
usage increase under both cases then.

So I just checked our SP2 code for setting DialogResult vs. calling Close
and it is exactly the same. Are you sure there isn't some other place
where there is different code getting run under these cases?

Feel free to post all the code or send me a repro and I'll look at it.

-Katie

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| From: "Martin Robins" <martin - robins @ ntlworld dot com>
| References: <14767AA3-7CA3-4600...@microsoft.com>
<KmDKLAh4...@cpmsftngxa07.phx.gbl>
<149EF5E1-B997-424E...@microsoft.com>
<FY3PKMI...@cpmsftngxa07.phx.gbl>

<e1ZfO8C7...@TK2MSFTNGP10.phx.gbl>
<a9m28JP7...@cpmsftngxa07.phx.gbl>


| Subject: Re: Memory Leak in Form

| Date: Sat, 7 Feb 2004 13:17:30 -0000
| Lines: 357


| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165

| Message-ID: <eZmC6yX7...@TK2MSFTNGP10.phx.gbl>


| Newsgroups: microsoft.public.dotnet.framework.compactframework
| NNTP-Posting-Host: cpc3-shep1-5-0-cust37.nott.cable.ntl.com 213.105.123.37
| Path:
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.
phx.gbl
| Xref: cpmsftngxa07.phx.gbl

microsoft.public.dotnet.framework.compactframework:45188
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

Alex Feinman [MVP]

ulæst,
13. feb. 2004, 14.30.3113.02.2004
til
Martin,

I've looked carefully into the implementation details, and from what I see,
I was wrong and there should be no difference between calling Close and
setting DialogResult provided the form is displayed via ShowDialog. My
information was outdated - from the beta timeframe. The form is not
destroyed and it is your responsibility to call Dispose when you are done or
make sure you have no outstanding references to the form to allow the GC to
collect it. This is different from pre-SP2 where you must have called
Dispose, because the GC was prevented from collecting an instance of a form
via internal reference that never cleared.

Can you confirm that you are using SP2?


"Martin Robins" <martin - robins @ ntlworld dot com> wrote in message

news:OkqMeuj7...@TK2MSFTNGP12.phx.gbl...

0 nye opslag