Skip to content

Instantly share code, notes, and snippets.

@mhutch

mhutch/crashy.cs Secret

Created October 11, 2016 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhutch/6a372cea19fab93df18ee99df5f4941c to your computer and use it in GitHub Desktop.
Save mhutch/6a372cea19fab93df18ee99df5f4941c to your computer and use it in GitHub Desktop.
using System;
using AppKit;
using Foundation;
namespace HelloMac
{
//TO REPRO:
// 1. Click on the botton
// 2. Close the window that pops up
// May have to repeat 2-3 times
public partial class ViewController : NSViewController
{
public ViewController (IntPtr handle) : base (handle) { }
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
base.View.AddSubview (NSButton.CreateButton ("Crash me", OpenChildWindow));
}
void OpenChildWindow ()
{
var windowDelegate = new CrashyDelegate ();
var frame = View.Window.Frame;
frame.Offset (20, 20);
NSWindow win = new NSWindow (frame, NSWindowStyle.Titled | NSWindowStyle.Closable | NSWindowStyle.Resizable, NSBackingStore.Buffered, true) {
WeakDelegate = windowDelegate
};
NSApplication.SharedApplication.RunModalForWindow (win);
GC.Collect ();
}
class CrashyDelegate : NSWindowDelegate
{
public override void WillClose (NSNotification notification)
{
// uncomment this line to fix the crash
// notification.DangerousRetain ();
NSApplication.SharedApplication.StopModal ();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment