Search code examples
iosxamarin.iosgarbage-collectiondispose

MonoTouch SIGSEGV during garbage collector initiated Dispose on ABPeoplePickerNavigationController


Building against MT 6.0.4. Targeting iOS 4. Testing on an iPhone 4S.

I'm stumped as to what I am doing wrong. I display the ABPeoplePickerNavigationController modally after subscribing to the SelectPerson event. Within the event, I take a copy of the data I need (only holding references to strings, not to any of the AdressBook or Person instances), and then close the modal dialog:

private string selectedPersonFirstName;
private string selectedPersonEmail;
private string selectedPersonPhone;

private void SelectContact()
{
    var peoplePicker = new ABPeoplePickerNavigationController();
    peoplePicker.Cancelled += (sender, e) => 
    {
        selectedPersonFirstName = null;
        selectedPersonEmail = null;
        selectedPersonPhone = null;
        peoplePicker.DismissViewController(true, null);
    };
    peoplePicker.SelectPerson += (sender, e) => 
    {
        var selectedName = String.Format("{0} {1}", e.Person.FirstName, e.Person.LastName);
        selectedPersonFirstName = e.Person.FirstName;
        if ( e.Person.GetEmails().Count > 0)
            selectedPersonEmail = e.Person.GetEmails().FirstOrDefault().Value;
        if ( e.Person.GetPhones().Count > 0)
            selectedPersonPhone = e.Person.GetPhones().FirstOrDefault().Value;

        peoplePicker.DismissViewController(true, null);
    };

    PresentViewController(peoplePicker, true, null);
}

At some point later when the GC kicks in, it crashes:

Stacktrace:

  at MonoTouch.Foundation.NSObject.FinishDispose () [0x0000b] in /Developer/MonoTouch/Source/monotouch/src/Foundation/NSObject.cs:158
  at MonoTouch.Foundation.NSObject/MonoTouch_Disposer.Drain (MonoTouch.Foundation.NSObject) [0x00062] in /Developer/MonoTouch/Source/monotouch/src/Foundation/NSObject.cs:376
  at (wrapper runtime-invoke) object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr) <0xffffffff>
  at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
  at MyApp.iOS.Application.Main (string[]) [0x00000] in /Users/tyson/Code/MyApp/iOS/Main.cs:19
  at (wrapper runtime-invoke) object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr) <0xffffffff>

Native stacktrace:

    0   MyAppName---------------            0x00f574f4 mono_handle_native_sigsegv + 280
    1   MyAppName---------------            0x00f3a2b8 mono_sigsegv_signal_handler + 268
    2   libsystem_c.dylib                   0x31ae97ed _sigtramp + 48
    3   CoreFoundation                      0x3745b2e7 CFRelease + 94
    4   AddressBookUI                       0x373922d1 -[ABGroupWrapper dealloc] + 100
    5   libobjc.A.dylib                     0x35a14175 _objc_rootRelease + 36
    6   AddressBookUI                       0x373a3f57 -[ABModel dealloc] + 146
    7   libobjc.A.dylib                     0x35a14175 _objc_rootRelease + 36
    8   AddressBookUI                       0x373aec43 -[ABAbstractViewController dealloc] + 38
    9   AddressBookUI                       0x373aeb3d -[ABMembersViewController dealloc] + 216
    10  libobjc.A.dylib                     0x35a14175 _objc_rootRelease + 36
    11  CoreFoundation                      0x3745b2e7 CFRelease + 94
    12  CoreFoundation                      0x3747106b -[__NSArrayM dealloc] + 122
    13  libobjc.A.dylib                     0x35a14175 _objc_rootRelease + 36
    14  UIKit                               0x33c891b9 -[UIViewController dealloc] + 496
    15  UIKit                               0x33c88f2f -[UINavigationController dealloc] + 198
    16  AddressBookUI                       0x373adcbd -[ABPeoplePickerNavigationController dealloc] + 376
    17  libobjc.A.dylib                     0x35a14175 _objc_rootRelease + 36
    18  MyAppName---------------            0x001e1ccc wrapper_managed_to_native_MonoTouch_ObjCRuntime_Messaging_void_objc_msgSend_intptr_intptr + 68
    19  MyAppName---------------            0x001ad49c MonoTouch_Foundation_NSObject_MonoTouch_Disposer_Drain_MonoTouch_Foundation_NSObject + 364
    20  MyAppName---------------            0x006997b8 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 200
    21  MyAppName---------------            0x00f3c6a4 mono_jit_runtime_invoke + 1644
    22  MyAppName---------------            0x00ff5dc0 mono_runtime_invoke + 128
    23  MyAppName---------------            0x00f2c6ec native_to_managed_trampoline_MonoTouch_Foundation_NSObject_MonoTouch_Disposer_Drain + 280
    24  CoreFoundation                      0x374a0eef +[NSObject performSelector:withObject:] + 42
    25  Foundation                          0x37d51747 __NSThreadPerformPerform + 350
    26  CoreFoundation                      0x374e6ad3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 14
    27  CoreFoundation                      0x374e629f __CFRunLoopDoSources0 + 214
    28  CoreFoundation                      0x374e5045 __CFRunLoopRun + 652
    29  CoreFoundation                      0x374684a5 CFRunLoopRunSpecific + 300
    30  CoreFoundation                      0x3746836d CFRunLoopRunInMode + 104
    31  GraphicsServices                    0x32375439 GSEventRunModal + 136
    32  UIKit                               0x33bece7d UIApplicationMain + 1080
    33  MyAppName---------------            0x001e6ca4 wrapper_managed_to_native_MonoTouch_UIKit_UIApplication_UIApplicationMain_int_string___intptr_intptr + 240
    34  MyAppName---------------            0x00ee0210 MyApp_iOS_Application_Main_string__ + 152
    35  MyAppName---------------            0x006997b8 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 200
    36  MyAppName---------------            0x00f3c6a4 mono_jit_runtime_invoke + 1644
    37  MyAppName---------------            0x00ff5dc0 mono_runtime_invoke + 128
    38  MyAppName---------------            0x00ffa224 mono_runtime_exec_main + 436
    39  MyAppName---------------            0x00fff770 mono_runtime_run_main + 756
    40  MyAppName---------------            0x00f434a4 mono_jit_exec + 140
    41  MyAppName---------------            0x0105c04c main + 2028
    42  MyAppName---------------            0x0001bd18 start + 40

=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

Has anyone seen anything like this before? I'm up for workarounds at this point as I need it more reliable for a demo ASAP (not for app store submission yet).


Solution

  • It looks like a bug. Something gets released one time too many and crash.

    You can, for your demonstration purpose, work around it by defining CFRetain like this:

    [System.Runtime.InteropServices.DllImport ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation")]
    static extern IntPtr CFRetain (IntPtr obj);
    

    and use it on the _AddressBook, like that

    peoplePicker.SelectPerson += (sender, e) => {
        CFRetain (peoplePicker._AddressBook);
        var selectedName = String.Format("{0} {1}", e.Person.FirstName, e.Person.LastName);
        ...
    

    That will balance the count and avoid the crash.

    Update: bug filled, you can c.c. yourself the the bug report to know when a fix will be released.