Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MonoMac window closing without error

I'm just starting out with MonoMac in Xamarin Studio, and I've run into the strangest problem:

I a window with an NSButton and a NSTextField on it. By this point I've cut out the event handler on the button, so it doesn't DO anything, except highlight when I click it. The button creation code looks like this:

nsButton = new NSButton(new System.Drawing.RectangleF(0, 0, 100, 100));
nsButton.BezelStyle = NSBezelStyle.RoundRect;
nsButton.Font = NSFont.SystemFontOfSize(
    NSFont.SystemFontSizeForControlSize(NSControlSize.Regular));
nsButton.StringValue = text;

...and then it gets added to the window like so:

nsView.AddSubview(control.Handle as NSView);

(because in this part of the code, control.Handle is typed as object, and nsView is the main view on the window).

All runs and works fine at first. But, if I click repeatedly on that button, eventually the window just closes. No error, no exception, and the app itself doesn't quit; menus continue to respond and cheerfully log messages when I use them. But the window is simply -- gone.

It's extremely repeatable: it happens after 21 clicks. If I add an event handler that updates the NSTextField (e.g. hello.Caption="Foo";), then it happens after 19 clicks. It doesn't matter whether I click fast or slow; it's always the same number of clicks. Note that there is no code in the project to close the window, and the window doesn't even have a close box; I know of no legitimate way to close it short of quitting the app.

I am baffled here, and don't know how to debug this further. Does Xamarin have some sort of evaluation limit that closes your windows after so-many events? Is it a framework bug? Any insight will be greatly appreciated.

like image 338
Joe Strout Avatar asked Sep 06 '26 06:09

Joe Strout


1 Answers

But, if I click repeatedly on that button, eventually the window just closes. No error, no exception, and the app itself doesn't quit; menus continue to respond and cheerfully log messages when I use them. But the window is simply -- gone.

This "disappearing without a trace" sometimes occurs when an application crashes in native code badly enough. This can occur due to bugs in the binding code or mistakes made in calling the native APIs that corrupt internal cocoa state. I believe you are using MonoMac, and that this particular issue has been fixed in Xamarin.Mac.

You can sometimes get more information from the output window or by attaching lldb to your process.

like image 175
chamons Avatar answered Sep 07 '26 19:09

chamons



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!