Removing reference created by ShowDialog(owner) - memory 'leak'

G

Guest

Hi, I have a lingering reference to a form, that is created by a call to
ShowDialog - what I'd like to know is how to remove the reference.

Eg.

Form1.cs

private void button1_Click(object sender, System.EventArgs e)
{
long memBefore = System.GC.GetTotalMemory(true);
Form2 f2 = new Form2();
f2.ShowDialog(this);
this.RemoveOwnedForm(f2);
f2.Dispose();
f2 = null;
System.GC.Collect();
long memAfter = System.GC.GetTotalMemory(true);
this.label1.Text = "B4:"+memBefore+" After:"+memAfter;
}


.....

Form2.cs

private void Form2_Load(object sender, System.EventArgs e)
{
long memBefore = System.GC.GetTotalMemory(true);
li = new ArrayList();
for(int i=0; i<1000000; i++)
li.Add("zzzzzzzzzzzzzzz");
long memAfter = System.GC.GetTotalMemory(true);
this.label1.Text = "B4:"+memBefore+" After:"+memAfter;
}


When this is run it reports a signficant difference in memory usage (4MB)
from the start to the finish (form2 opened and closed). Reason being
(according to a mem. profiler) is a lingering reference to Form2 created by
the call to ShowDialog, which is preventing the GC from freeing up the memory
used in Form2.


The memory profiler reports the reference is from the call stack:
Form.AddOwnedForm(Form)
Form.set_Owner(Form)
Form.ShowDialog(IWin32Window)


this is why I added the call to this.RemoveOwnedForm(f2); which didn't help.





If you change ShowDialog to just;

ShowDialog()

with no owner specified, the memory is freed up.





I realise that I can null the reference to the ArrayList in the Form2
Dispose method, but my question is, should I have to?

Any input welcome

Jim
 
J

Jeffrey Tan[MSFT]

Hi Jim,

Thanks for your post.

Yes, after testing your detailed sample code, I can reproduce out this
problem. Form.Dispose method actually invokes RemoveOwnedForm. So, It seems
that RemoveOwnedForm method did not remove the form reference correctly.

Currently, I can not think of a good workaround for this problem. Can we
pass null reference for ShowDialog method?

Also, I found that someone has reported this issue to microsoft. And this
problem has been fixed in VS.net2005. Please refer to the link below:
"Bug Details: Owner form does not release reference to child form when
using ShowDialog(owner)"
http://lab.msdn.microsoft.com/ProductFeedback/viewFeedback.aspx?feedbackid=e
4bf29a3-94e5-457c-bb90-759a02ec0db1

Thanks for your reporting.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Jim,

Does my reply make sense to you? If you still have any concern, please feel
free to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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