Custom NSButton click handler instead of .Activated caused strage crashes

Hi All,

There is this bug:
Bug 19619 - Multiple event handlers on button press does not work on Mac, works on iOS

And I've written the following workaround for it:

public class MyButton: NSButton
{
//...

public MyButton()
{
    //...
    InitClick();
}

#region Click (framework bug workaround)

// Framework bug: Activated only accepts 1 handler. See: https://bugzilla.xamarin.com/show_bug.cgi?id=19619
// So, we only subscribe 1 fn to Activated, in which we fire the custom Clicked event instead
public event EventHandler Clicked;

private void InitClick()
{
    Activated += (sender, e) => {
        var handler = Clicked;
        if(handler != null)
            handler(sender, e);
    };
}

#endregion

}

This workaround (seems to) crash my app sometimes. Sometimes means, not immediately, just if you've already clicked enough.
So, I had a surface with ~6 Buttons of this kind; not all of them had Clicked subscribers. After clicking on them multiple times, randomly, they will have native crashes. The strangest thing is, if I'm clicking on the ones not having subscribers, the app will crash faster/more frequently.

So, I rollbacked this "workaround", which caused me a lot of head ache; and to be honest, I don't need another, because I can see in the above bug report this problem planned to be fixed soon in XM 2.0.x.

I'm only very curious. Why did this happen? I wouldn't think the above code could cause such errors...

Some stack traces attached:

2015-04-28 15:09:01.722 Emasos.IQ.Mac[770:12985] -[NSString __monomac_internal_ActionDispatcher_activated:]: unrecognized selector sent to instance 0x608000012ed0
2015-04-28 15:09:01.723 Emasos.IQ.Mac[770:12985] -[NSString __monomac_internal_ActionDispatcher_activated:]: unrecognized selector sent to instance 0x608000012ed0
2015-04-28 15:09:01.724 Emasos.IQ.Mac[770:12985] (
0 CoreFoundation 0x00007fff90faf66c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff9776a76e objc_exception_throw + 43
2 CoreFoundation 0x00007fff90fb26dd -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00007fff90ef9aa4 ___forwarding___ + 1028
4 CoreFoundation 0x00007fff90ef9618 _CF_forwarding_prep_0 + 120
5 libsystem_trace.dylib 0x00007fff8fe39cd7 _os_activity_initiate + 75
6 AppKit 0x00007fff95621b71 -[NSApplication sendAction:to:from:] + 452
7 AppKit 0x00007fff95621970 -[NSControl sendAction:to:] + 86
8 AppKit 0x00007fff957f786c __26-[NSCell _sendActionFrom:]_block_invoke + 131
9 libsystem_trace.dylib 0x00007fff8fe39cd7 _os_activity_initiate + 75
10 AppKit 0x00007fff9566a509 -[NSCell _sendActionFrom:] + 144
11 libsystem_trace.dylib 0x00007fff8fe39cd7 _os_activity_initiate + 75
12 AppKit 0x00007fff95685085 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2775
13 AppKit 0x00007fff956842b9 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 491
14 AppKit 0x00007fff95683899 -[NSControl mouseDown:] + 714
15 AppKit 0x00007fff95b81a18 -[NSWindow _reallySendEvent:] + 12721
16 AppKit 0x00007fff9560816e -[NSWindow sendEvent:] + 446
17 AppKit 0x00007fff955ba451 -[NSApplication sendEvent:] + 4183
18 AppKit 0x00007fff95446608 -[NSApplication run] + 711
19 AppKit 0x00007fff95431a14 NSApplicationMain + 1832
20 ??? 0x000000010891f05b 0x0 + 4438749275
21 ??? 0x000000010891eef2 0x0 + 4438748914
22 Emasos.IQ.Mac 0x00000001000f6988 mono_jit_runtime_invoke + 1592
23 Emasos.IQ.Mac 0x000000010019404e mono_runtime_invoke + 110
24 Emasos.IQ.Mac 0x0000000100199658 mono_runtime_exec_main + 376
25 Emasos.IQ.Mac 0x0000000100009e8e main + 862
26 Emasos.IQ.Mac 0x0000000100001474 start + 52
27 ??? 0x0000000000000001 0x0 + 1
)

Sometimes I got the same, just not with NSString on the top, but CUIImage; guess it have depended on which Button caused the crash.

Regards,
Norbert

Best Answer

Answers

  • NorbertVirthNorbertVirth USMember

    Thank you for the fast answer. I'm using the stable channel, so Xamarin.Mac, Version: 1.12.0.14 (Business Edition)

Sign In or Register to comment.