Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
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
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 on
GitHub or Developer Community with
your current version information, steps to reproduce, and relevant error
messages or log files if you are hitting an issue that looks similar to
this resolved bug and you do not yet see a matching new report.
Created attachment 14978 [details]
I am attaching a test case (compile with 'mcs formsthreads.cs -r:System.Windows.Forms.dll'). This test case works under native .Net and fails with Mono.
The test shows the first forms successfully from a newly created thread, then the second form creation fails with the following stack trace:
System.OutOfMemoryException: Not enough memory to complete operation [GDI+ status: OutOfMemory]
at System.Drawing.GDIPlus.CheckStatus (Status status) <0x29fa710 + 0x0023a> in <filename unknown>:0
at System.Drawing.Graphics.FromHwnd (IntPtr hwnd) <0x2a21bd0 + 0x0041b> in <filename unknown>:0
at System.Windows.Forms.XplatUIWin32.GetAutoScaleSize (System.Drawing.Font font) <0x2a21a70 + 0x0002d> in <filename unknown>:0
at System.Windows.Forms.XplatUI.GetAutoScaleSize (System.Drawing.Font font) <0x2a21a10 + 0x0002e> in <filename unknown>:0
at System.Windows.Forms.Form.GetAutoScaleSize (System.Drawing.Font font) <0x2a219b0 + 0x00027> in <filename unknown>:0
at System.Windows.Forms.Form..ctor () <0x29ea340 + 0x0010a> in <filename unknown>:0
at (wrapper remoting-invoke-with-check) System.Windows.Forms.Form:.ctor ()
at FormsThreadsTest.GuiThread () <0x29e3280 + 0x0004c> in <filename unknown>:0
at FormsThreadsTest.Main () <0x29e0f10 + 0x000b7> in <filename unknow')
'OutOfMemory' status from GDI+ is due to GdipCreateFromHwnd is supplied with invalid window handle ('FosterWindow' window is closed by the time of the second form creation).
The following scenario happens there:
1. Forms framework is first called from a thread, System.Windows.Forms.XplatUIWin32 ctor is called from it and FosterWindow is created
2. Thread exits, FosterWindow is closed automatically on thread termination
3. As XplatUI is initialized statically the subsequent calls to XplatUI use FosterWindow created previously which is already dead.
I've got this case in a real application (which is native but using .Net from mixed and pure .Net dlls from different threads).