-1

We've been building messenger like app with share extension (from safari). Right now we've encountered a big problem Extension crashes when im trying to get url string

CRASH:

Tread 1

Unhandled Exception:

0 Mono 0x01746691 mono_handle_exception_internal + 2168

1 Mono 0x01745e13 mono_handle_exception + 30

2 Mono 0x0173ed2d mono_arm_throw_exception + 104

3 com.XXX.ShareExtension 0x00268e44 throw_exception + 64

at ObjCRuntime.Runtime.throw_ns_exception (intptr) [0x00000] in /Users/builder/data/lanes/2077/d8e9592a/source/maccore/runtime/Delegates.generated.cs:100 at (wrapper native-to-managed) ObjCRuntime.Runtime.throw_ns_exception (intptr) <0x00048>

6 com.XXX.ShareExtension 0x006fe5c0 xamarin_throw_ns_exception + 52

7 com.XXX.ShareExtension 0x007004b0 _ZL17exception_handlerP11NSException + 224

8 CoreFoundation 0x242cbba9 + 644

9 libobjc.A.dylib 0x35c7f087 + 174

10 libc++abi.dylib 0x35463e17 + 78

11 libc++abi.dylib 0x354638f7 __cxa_rethrow + 102

12 libobjc.A.dylib 0x35c7ef47 objc_exception_rethrow + 42

Which means mono crashes. Looking further I see:

Tread 2

iOS_ShareExtension[336] : Unhandled managed exception:

Objective-C exception thrown. Name: NSInvalidArgumentException Reason:

NSGetSizeAndAlignment(): unsupported type encoding spec 'Y' at 'Y„ hŸ0iŸêoŸû' in 'Y„ hŸ0iŸêoŸû'

Native stack trace:

0 CoreFoundation 0x242cb883 + 150

1 libobjc.A.dylib 0x35c7edff objc_exception_throw + 38

2 CoreFoundation 0x242cce71 + 536

3 CoreFoundation 0x242cef69 + 60

4 CoreFoundation 0x241f9515 + 516

5 libextension.dylib 0x355c2e47 + 150

6 com.XXX.ShareExtension 0x000643f4 wrapper_managed_to_native_ObjCRuntime_Messaging_void_objc_msgSend_IntPtr_IntPtr_IntPtr_intptr_intptr_intptr_intptr_intptr + 128

7 com.XXX.ShareExtension 0x0003a5e8 Foundation_NSItemProvider_LoadItem_string_Foundation_NSDictionary_System_Action_2_Foundation_NSObject_Foundation_NSError + 384

8 com.XXX.ShareExtension 0x0000c5d4 iOS_ShareExtension_ShareViewController_ViewDidLoad + 1048

My code that crashes:

    NSExtensionItem content = ExtensionContext.InputItems [0];

    foreach (NSItemProvider item in content.Attachments) {
        if (item.HasItemConformingTo ("public.url")){
            item.LoadItem (UTType.URL,null, OnItemLoaded);
            break;
        }
    }

and then:

NSUrl url = objectLoaded as NSUrl;
_sharedURl = url.AbsoluteString; // THIS CRASH!!!

Any idea how to bypass this? I've even tried coverting this NSUrl in various ways.

  • What is "objectLoaded"? If it is the data received in the LoadItem completionHandler then it is not a NSUrl but the data that the Url returned. Type casting it to a url and trying to access AbsoluteString will cause a fault (your stack trace appears to show binary data for it) – SushiHangover Sep 29 '15 at 16:18
  • Yes, it is: private void OnItemLoaded(NSObject objectLoaded, NSError error) It seems you are right, now I wonder, why it sometimes works? – DivisionByNullPointer Sep 29 '15 at 18:14

1 Answers1

0

This may be addressed by this fix: https://bugzilla.xamarin.com/show_bug.cgi?id=34518

I would double check that it's actually getting to the line you think is causing the problem (using Console.WriteLine because debug attachment doesn't work for extensions). If objectLoaded was not an NSUrl, the cast will return null and I would expect a null pointer exception rather than an obscure encoding error.

EDIT: The referenced bug fix has not made it's way through to the Xamarin alpha channel yet, but the problem appears to be fixed by making the callback method public. In my case I was using an inline delegate, but that's just a private method "behind the scenes".

Rupert Rawnsley
  • 2,622
  • 1
  • 29
  • 40