Bug 38614 - Form creation fails when System.Windows.Forms.XplatUIWin32 was initialized from thread which is already finished
Summary: Form creation fails when System.Windows.Forms.XplatUIWin32 was initialized fr...
Alias: None
Product: Class Libraries
Classification: Mono
Component: Windows.Forms ()
Version: master
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2016-02-10 10:02 UTC by Paul Gofman
Modified: 2016-02-10 23:13 UTC (History)
3 users (show)

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

Test case (420 bytes, text/x-csharp)
2016-02-10 10:02 UTC, Paul Gofman

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

Related Links:

Description Paul Gofman 2016-02-10 10:02:44 UTC
Created attachment 14978 [details]
Test case

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).
Comment 1 Alexander Köplinger [MSFT] 2016-02-10 23:13:12 UTC
Merged https://github.com/mono/mono/pull/2591