Bug 45298 - Crash when NSWindow over-released
Summary: Crash when NSWindow over-released
Status: VERIFIED FIXED
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Library (Xamarin.Mac.dll) ()
Version: Master
Hardware: PC Mac OS
: High normal
Target Milestone: (C9)
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-10-11 22:31 UTC by Mikayla Hutchinson [MSFT]
Modified: 2016-10-14 11:11 UTC (History)
3 users (show)

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

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 Developer Community or GitHub 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:
Status:
VERIFIED FIXED

Description Mikayla Hutchinson [MSFT] 2016-10-11 22:31:59 UTC
To repro:

Use this to replace the ViewController in a new XM project.

https://gist.github.com/mhutch/6a372cea19fab93df18ee99df5f4941ca
Comment 1 Mikayla Hutchinson [MSFT] 2016-10-11 22:32:43 UTC
XM Version: 3.0.0.51
Comment 2 Mikayla Hutchinson [MSFT] 2016-10-11 22:52:31 UTC
NSZombieEnabled indicates the issue is [NSWindow retainCount]
Comment 3 Mikayla Hutchinson [MSFT] 2016-10-11 22:58:47 UTC
It looks like the issue here may be NSWindow.ReleaseWhenClosed defaulting to true.
Comment 5 Mikayla Hutchinson [MSFT] 2016-10-12 20:02:15 UTC
Sorry, bad gist link. It's actually https://gist.github.com/mhutch/6a372cea19fab93df18ee99df5f4941c
Comment 6 Chris Hamons 2016-10-12 21:10:21 UTC
If you use my favorite GC helper method:

			new System.Threading.Thread (() =>
				{
					while (true)
					{
						System.Threading.Thread.Sleep (1000);
						GC.Collect ();
					}
				}).Start ();

it crashes in one button press.

Looking into it now.
Comment 7 Chris Hamons 2016-10-12 21:57:29 UTC
It appears that windows allocated via new NSWindow () differ both from windows rehydrated from storyboards and those created via an NSWindowController.

Creation                                                 Initial ReleasedWhenClosed state
------------------------------------------------------------------------
new NSWindow ()                                              True
Storyboard AwakeFromNib                                 False
new NSWindowController() / .Window                 False

Unfortunately there are a number of places where the "default" settings a newly init'ed Cocoa objects give are less than optimal, and this is the worst by far.


We could hack NSWindow's init constructors to set ReleasedWhenClosed = false, but I'm somewhat fearful of backwards compatibility.

We could protect it with a global flag, just like NSApplication.CheckForIllegalCrossThreadCalls, when we changed behavior.

NSWindow.DisableSafeConstructors?

In any case, setting ReleasedWhenClosed = false "fixes" things.
Comment 8 Chris Hamons 2016-10-13 17:23:08 UTC
https://github.com/xamarin/xamarin-macios/pull/990
Comment 9 Chris Hamons 2016-10-13 19:17:49 UTC
Fixed the 95% use case new NSWindow () with:

https://github.com/xamarin/xamarin-macios/commit/47e10e3063864469b24d0ca304ec031f209e756d

If we get more reports of xib files with this set, we can look into more complicated fixes for those.
Comment 10 Danish Akhtar 2016-10-14 11:10:16 UTC
I am able to reproduce this issue with X.mac xamarin.mac-3.0.0.92. However with latest C9 X.mac its working fine, NS window is not getting crashed. Here is the screencast for the same: http://www.screencast.com/t/LJy9hWVvIO

Hence closing this issue.
Comment 11 Danish Akhtar 2016-10-14 11:11:03 UTC
latest C9 X.mac is 3.0.0.130