I am working in mono on OSX10.6 - but this question is for Cocoa experts also please.
I have a context menu which I attach to an NSTableView. When I select the context menu the handler (which is empty for test) is called ok but then the application crashes.
This is the code I have in the window controller:
...
myNSTableView.Menu = new NSMenu();
NSMenuItem mi = new NSMenuItem("menu item 1", myHandler);
myNSTableView.Menu.AddItem(mi);
...
public void myHandler(object Sender, EventArgs e)
{
}
I also have an NSTimer setup to repeat every 200ms
myNSTimer = NSTimer.CreateRepeatingScheduledTimer(new TimeSpan(2000000),delegate {
move an NSSlider
});
NSRunLoop.Main.AddTimer(myNSTimer, NSRunLoopMode.Common);
NSRunLoop.Main.AddTimer(myNSTimer, NSRunLoopMode.EventTracking);
As soon as I try to access the context menu, the application crashes - every time.
If I dont start the timer, the context menu is fine.
I have tried literally everything to get this to work and have run out of ideas, so I really need someones help please, even just a pointer to where the problem might be. Forgetting about the fact that its mono just now, can anyone see from a cocoa point of view a problem?
It turned out that the problem was exacerbated by the timer, but the cause was actually that I wasn't declaring instance variables for the NSMenuItems, so they were getting garbage collected and causing crash at objc level.