Bug 5639 - Exception disposing of NSWindow subclass
Summary: Exception disposing of NSWindow subclass
Alias: None
Product: MonoMac
Classification: Desktop
Component: Bindings ()
Version: GIT
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2012-06-13 14:53 UTC by Jon Lipsky
Modified: 2015-02-12 18:45 UTC (History)
3 users (show)

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

Sample MonoMac project to reproduce the issue. Simply run the sample. The main screen describes what to do. (23.32 KB, application/zip)
2012-06-13 14:53 UTC, Jon Lipsky

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:

Description Jon Lipsky 2012-06-13 14:53:05 UTC
Created attachment 2050 [details]
Sample MonoMac project to reproduce the issue.  Simply run the sample.  The main screen describes what to do.

I'm getting random crashes in my MonoMac application in the "NSObject.MonoMac_Disposer.Drain" method:

 at (wrapper managed-to-native) MonoMac.ObjCRuntime.Messaging.void_objc_msgSendSuper (intptr,intptr) <IL 0x00024, 0xffffffff>
  at MonoMac.Foundation.NSObject/MonoMac_Disposer.Drain (MonoMac.Foundation.NSObject) [0x0003b] in /Development/github/monomac/src/Foundation/NSObject.cs
  at (wrapper dynamic-method) object.[MonoMac.Foundation.NSObject+MonoMac_Disposer.Void Drain(MonoMac.Foundation.NSObject)] (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector,MonoMac.Foundation.NSObject) <IL 0x00011, 0x0004b>
  at (wrapper native-to-managed) object.[MonoMac.Foundation.NSObject+MonoMac_Disposer.Void Drain(MonoMac.Foundation.NSObject)] (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector,MonoMac.Foundation.NSObject) <IL 0x000b6, 0xffffffff>
  at (wrapper managed-to-native) MonoMac.AppKit.NSApplication.NSApplicationMain (int,string[]) <IL 0x0009d, 0xffffffff>

By building my own version of MonoMac, I've been able to figure out that this crash always occurs when it's trying to dispose of the "super_handle" of my NSWindow subclass.

A sample is attached that does the following to reproduce the issue:

1) Create a new MonoMac document based project.

2) Create a subclass of NSWindow:

using System;
using System.Collections.Generic;
using System.Drawing;
using MonoMac.AppKit;

namespace DisposalProblem
	public class TestWindow : NSWindow
		public TestWindow () : base(new RectangleF(100,100,500,500),NSWindowStyle.Resizable | NSWindowStyle.Closable, NSBackingStore.Buffered, false)
		public TestWindow (IntPtr aIntPtr) : base(aIntPtr)

3) Use interface builder to have one of the pull down menus send the message "showTestWindow:".

4) Add the following to your AppDelegate class:

		private TestWindow testWindow;
		public void ShowTestWindow()
			if (testWindow == null)
				testWindow = new TestWindow();
				testWindow.WillClose += HandleWillClose;

		void HandleWillClose (object sender, EventArgs e)
			testWindow.WillClose -= HandleWillClose;
			testWindow = null;

5) Run the application, and open and close the window using the "wired up" menu item multiple times until it crashes.  Should take less than 20 seconds to make it crash.
Comment 1 Jon Lipsky 2012-06-19 13:02:18 UTC
I found the cause of this, so I'm not sure if this is really a bug, or even if this is a case that MonoMac should handle.

I had forgotten that NSWindow has a property called "ReleasedWhenClosed", which is set to true by default.  Once I set that to false, then everything works fine.

Ideally, it would be nice if the MonoMac disposer could detect that the native object has already been released to prevent the exception above to be called.
Comment 2 Martin Baulig 2012-08-20 04:12:50 UTC
Haven't looked at this yet, but this may be related to the problem in bug #1086:
native code takes ownership of the object, but the managed garbage collector is not aware of that.
Comment 3 Timothy Risi 2015-02-12 18:45:42 UTC
I downloaded the sample and tested it on the latest stable builds and was unable to reproduce the crash.