Question

I recently posted about a bug in MonoMac in which the window would abruptly disappear after clicking on a button 20 times or so. That bug, it turned out, doesn't seem to affect Xamarin.Mac, so I switched my project to that.

But now I'm seeing virtually the same bug in a different context: after typing a few lines of text into an NSTextField, the window disappears in exactly the same manner. No error, no exception; it just vanishes. Poof, gone!

I've reproduced this in a trivial project: you can see it yourself by creating a new empty Xamarin.Mac project, adding this code in a new file, and running. Then mash the keyboard a little while. After 5 or 6 lines, the window disappears.

(Note that the app menus continue to work, writing log messages and executing my code when I select the menu commands. So it doesn't appear as though the app itself has crashed.)

Curiously, this seems to only occur when the field is inside an NSView subclass where IsFlipped has been overridden. But it doesn't matter whether IsFlipped returns true or false -- if the method is there at all, the bug occurs; if commented out, the bug does not seem to occur.

So: Has anyone else run into this sort of bug in Xamarin.Mac? Perhaps in other contexts that will give us more clues as to a root cause? Any debugging tips for a Xamarin newbie?

(I know I could work around it in some projects by avoiding IsFlipped, but I worry that it would just pop up to bite me in some other way.)

Was it helpful?

Solution

OK, it turns out that this isn't a bug in Xamarin.Mac at all, but in my code. Because I wasn't retaining a reference to the NSWindow object, it went out of scope, and as soon as the garbage collector gets around to noticing it, it's disposed and the GUI window is torn down.

All we have to do is retain a reference to the window (for example, in the AppDelegate class), and the problem goes away.

Hats off to Chris Hamons at Xamarin, who jumped right on this and quickly found the problem for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top