MonoMac: Having issues with other dialogs

0

I'm writing an app in MonoMac and its having an issue where if I open a secondary dialog using this invocation

var prefForm = new PrefFormController();
prefForm.Window.MakeKeyAndOrderFront(mainWindowController);

The app ends up crashing if you switch enough back and forth from the main dialog and the secondary dialog, or buttons being clicked will end up crashing the application.

Loaded assembly: /Users/michaelmanley/Desktop/NasuTek-StreamDesk/StreamDesk-Cocoa/bin/Debug/StreamDesk.app/Contents/Resources/StreamDesk.exe
Loaded assembly: /Users/michaelmanley/Desktop/NasuTek-StreamDesk/StreamDesk-Cocoa/bin/Debug/StreamDesk.app/Contents/Resources/StreamDesk.Managed.dll
Loaded assembly: /Users/michaelmanley/Desktop/NasuTek-StreamDesk/StreamDesk-Cocoa/bin/Debug/StreamDesk.app/Contents/Resources/MonoMac.dll [External]
Loaded assembly: /Library/Frameworks/Mono.framework/Versions/2.10.8/lib/mono/gac/System.Core/4.0.0.0__b77a5c561934e089/System.Core.dll [External]
Loaded assembly: /Users/michaelmanley/Desktop/NasuTek-StreamDesk/StreamDesk-Cocoa/bin/Debug/StreamDesk.app/Contents/Resources/Mono.Addins.dll [External]
Loaded assembly: /Library/Frameworks/Mono.framework/Versions/2.10.8/lib/mono/gac/System/4.0.0.0__b77a5c561934e089/System.dll [External]
Loaded assembly: /Library/Frameworks/Mono.framework/Versions/2.10.8/lib/mono/gac/System.Xml/4.0.0.0__b77a5c561934e089/System.Xml.dll [External]
Loaded assembly: __MetadataTypes [External]
Loaded assembly: __MetadataTypes [External]
Loaded assembly: /Library/Frameworks/Mono.framework/Versions/2.10.8/lib/mono/gac/Mono.Addins.CecilReflector/0.6.0.0__0738eb9f132ed756/Mono.Addins.CecilReflector.dll [External]
Thread started: 
Unloaded assembly: /Library/Frameworks/Mono.framework/Versions/2.10.8/lib/mono/gac/Mono.Addins.CecilReflector/0.6.0.0__0738eb9f132ed756/Mono.Addins.CecilReflector.dll
Unloaded assembly: __MetadataTypes
Thread finished: 
Thread started: 
Loaded assembly: /Library/Frameworks/Mono.framework/Versions/2.10.8/lib/mono/gac/System.Configuration/4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll [External]
Thread started: 
Thread started: 
Loaded assembly: /Library/Frameworks/Mono.framework/Versions/2.10.8/lib/mono/gac/Mono.Security/4.0.0.0__0738eb9f132ed756/Mono.Security.dll [External]
Thread started: 
Thread started: 
Loaded assembly: ObjCImplementations [External]
Loaded assembly: /Library/Frameworks/Mono.framework/Versions/2.10.8/lib/mono/gac/System.Design/4.0.0.0__b03f5f7f11d50a3a/System.Design.dll [External]
Loaded assembly: /Library/Frameworks/Mono.framework/Versions/2.10.8/lib/mono/gac/System.Drawing/4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll [External]
Thread finished: 
Thread started: 
Thread started: 
Thread started: 
Thread started: <---- These pop up every time you go back to the secondary window from the primary window
Thread started: 
… 
Thread started: 
Thread started: 
Thread started: 
Stacktrace:

  at (wrapper managed-to-native) MonoMac.AppKit.NSApplication.NSApplicationMain (int,string[]) <IL 0x0009d, 0xffffffff>
  at MonoMac.AppKit.NSApplication.Main (string[]) [0x00000] in /cvs/monomac/src/AppKit/NSApplication.cs:74
  at StreamDesk.MainClass.Main (string[]) [0x0000f] in /Users/michaelmanley/Desktop/NasuTek-StreamDesk/StreamDesk-Cocoa/StreamDesk/Main.cs:22
  at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>

Native stacktrace:

    0   StreamDesk                          0x00094efc mono_handle_native_sigsegv + 284
    1   StreamDesk                          0x00004fe8 mono_sigsegv_signal_handler + 248
    2   libSystem.B.dylib                   0x006db05b _sigtramp + 43
    3   ???                                 0xffffffff 0x0 + 4294967295
    4   AppKit                              0x04b80255 -[NSControl sendAction:to:] + 108
    5   AppKit                              0x04b7bd02 -[NSCell _sendActionFrom:] + 169
    6   AppKit                              0x04b7aff9 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 1808
    7   AppKit                              0x04bd06ed -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 524
    8   AppKit                              0x04b79a4f -[NSControl mouseDown:] + 812
    9   AppKit                              0x04b77a58 -[NSWindow sendEvent:] + 5549
    10  AppKit                              0x04a9060b -[NSApplication sendEvent:] + 6431
    11  AppKit                              0x04a24253 -[NSApplication run] + 917
    12  AppKit                              0x04a1c289 NSApplicationMain + 574
    13  ???                                 0x0d7d9b76 0x0 + 226335606
    14  ???                                 0x0d7d9974 0x0 + 226335092
    15  ???                                 0x00dee048 0x0 + 14606408
    16  ???                                 0x00dee1be 0x0 + 14606782
    17  StreamDesk                          0x0000d282 mono_jit_runtime_invoke + 722
    18  StreamDesk                          0x001a436a mono_runtime_invoke + 170
    19  StreamDesk                          0x001a6f01 mono_runtime_exec_main + 705
    20  StreamDesk                          0x001a6111 mono_runtime_run_main + 929
    21  StreamDesk                          0x00069995 mono_jit_exec + 149
    22  StreamDesk                          0x0006bf13 mono_main + 9587
    23  StreamDesk                          0x00002299 main + 441
    24  StreamDesk                          0x000020a6 start + 54
    25  ???                                 0x00000001 0x0 + 1

The exception can differ, which makes debugging this issue hard, the only way I know i can reproduce it is by going back and forth from the Main Window, and back to the Secondary window, after a while, the application will terminate

Am I doing the wrong way to show a dialog, or is this a bug with Mono itself, but I dought Mono could be having a massive bug of this scale where you cant even display other windows.

c#
cocoa
mono
monomac
asked on Stack Overflow Feb 7, 2012 by DrHouse

1 Answer

3

The issue is that a managed object is getting freed before it's corresponding native object. Then the native object tries to call a method on the managed object, and the app crashes.

You can probably make it more reproducible by running the GC every second:

ThreadPool.QueueUserWorkItem ((v) =>
{
    while (true) {
        Thread.Sleep (1000);
        GC.Collect (GC.MaxGenerations);
    }
});

This is where things go wrong:

3   ???        0xffffffff 0x0 + 4294967295
4   AppKit     0x04b80255 -[NSControl sendAction:to:] + 108

it looks like the problem is with a NSControl (or derived) instance. Besides this it's hard to say which object is actually getting freed with the information available. The easiest way to figure it out is probably to keep objects artificially alive (adding them to a static list for instance), and then reduce the subset of the objects you keep alive until you've pinpointed the exact instance which is causing the crash.

Once you've found out which object are getting prematurely freed you'll have to ensure the GC doesn't free it too early (one method is to add it to a static list, but the best approach depends on each case).

Have in mind that the object doesn't have to be an object you create explicitly in your code, it can also be delegates or event handlers for instance.

answered on Stack Overflow Feb 7, 2012 by Rolf Bjarne Kvinge

User contributions licensed under CC BY-SA 3.0