Bug 33390 - When disposing controls objects including System.Windows.Forms.Hwnd are leaked
Summary: When disposing controls objects including System.Windows.Forms.Hwnd are leaked
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: Windows.Forms ()
Version: 4.2.0 (C6)
Hardware: PC Linux
: --- normal
Target Milestone: Community
Assignee: Bugzilla
Depends on:
Reported: 2015-08-26 05:40 UTC by Martin Akman
Modified: 2017-09-01 11:56 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Application demonstrating issue & Profiling results (30.23 KB, text/x-csharp)
2015-08-26 05:40 UTC, Martin Akman

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 33390 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Martin Akman 2015-08-26 05:40:15 UTC
Created attachment 12643 [details]
Application demonstrating issue & Profiling results

I am developing an long lived application, that ideally should not have to restart for weeks.  It was originally developed for C# Visual Studio to run under Windows.  But since taking up the project I have ported it to Mono under Debian Linux.  Because of this and the size of the application, I continued to use Windows.Forms to save rewriting all the UI in GTK.  During system testing before release a memory leak was noticed, and I have narrowed it down to be that Controls and Forms are not getting completely disposed of, even though I am calling Dispose() on them.

I created a simple application to see if it was my code or something in Mono, and I can't see any more ways of releasing the memory, so I am guessing something in the Windows.Forms is hanging on to the references.  The application works by creating a main form, which displays the Process.GetCurrentProcess().WorkingSet64 in the Form.Text property.  When you click on this form it spawns another form, which has 50 empty labels in it, so you see the leak a bit easier.  Clicking on this form, will close it, then the main form will dispose of this form and redisplay the current memory usage.  By clicking on these forms to open one then close it and repeat, the memory usage steadily climbs.

I have attached the application I created to demonstrate this issue to this bug report, with the results from heapshot profiling every 5 seconds, as a comment below the C# code.  This report was generate by running the application with heapshot profiling enabled, and constantly clicking on the forms to create and destroy forms.
Comment 1 alt.g4-01wcpnk 2015-11-13 09:16:44 UTC

Mono 4.0.5, Raspberry PI, Linux.

- Application compiled under Visual Studio 2012 using Visual basic and Framework 4.0.

- The main window of an application uses a PictureBox. Each time the property PictureBox.image is changed, about 300kb are lost. Some hours later, the system kills the app because the memory is full.

- I have tried different ways of filling PictureBox.image without any success.

I think that we experience the same issue.
Comment 2 Martin Akman 2015-11-13 18:22:16 UTC

I have worked out that your probably not going to get a reply from the developers here.  From what I have read, they have ceased development of WinForms as have Microsoft.  I ended up rewriting the GUI in GTK#, which fixed the issue, and the GUI is also a bit quicker.

Have you tried manually disposing the PictureBox.Image before changing it? Make sure you disposing of everything correctly (every disposable object, should be in a using statement or have .Dispose() called on it in all code paths), including where you load the image. If you think its a bug, your probably on your own with fixing it as well, but I was able to use the PictureBox with an images generated using NPlot with no memory leaks, I think ...

Use the Profiler to see what objects are actually leaking: http://www.mono-project.com/docs/debug+profile/profile/profiler/
Comment 3 alt.g4-01wcpnk 2015-11-14 05:39:14 UTC

Yes I have tried "image.dispose" each time I load a new one but it does not change anything. 

You say that they cease working on winforms but they bring a lot of nice features (datagridview) which are very usefull on the RPi. I need a cross platform compatibility and GTK is not so much mu cup of tea. I do need to switch on XAML ?

Will try the profiler, thank you.

Thanks for your help.
Comment 4 alt.g4-01wcpnk 2015-11-17 09:55:20 UTC
Tested under VC and C#, same behaviour :-(

In fact the image is linked to a field of a database. Each time the table is refreshed, the image changes, loosing at least 300kb...

This does not occur under Windows with Visual studio.
Comment 5 alt.g4-01wcpnk 2015-11-17 09:56:34 UTC
VB, not VC...
Comment 6 alt.g4-01wcpnk 2015-11-17 13:05:51 UTC
problem solved with version 4.2 on Raspberry no more leaking :-))