Mysterious crash: How to debug it?

I have encountered a strange crash, which seems to come from inside of MOE, but I am not sure. It is easily reproducible inside my project, but it does not seem to have a clear cause. When I click the Back button of my navigation controller under certain conditions, the app crashes with this in the output:

2017-08-30 21:52:45.677 AppNameRedacted[11817:5932886] -[java.lang.Long intValue]: unrecognized selector sent to instance 0x14606d110
2017-08-30 21:52:45.685 AppNameRedacted[11817:5932886] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[java.lang.Long intValue]: unrecognized selector sent to instance 0x14606d110'
*** First throw call stack:
(0x1818aadb0 0x180f0ff80 0x1818b1c4c 0x1818aebec 0x1817acc5c 0x186986f4c 0x186988394 0x186a235dc 0x186a968f0 0x186a96678 0x1843c6228 0x1843b08ac 0x1844c8360 0x1843afa88 0x1843917a8 0x184391494 0x184390ab8 0x184390818 0x184389ddc 0x181860728 0x18185e4cc 0x18185e8fc 0x181788c50 0x183070088 0x186a76088 0x104530044 0x1045446e0 0x104528af8 0x1045289a0 0x104529a2c 0x104544e64 0x1045301b4 0x107d824f4 0x1042473f0 0x10428df30 0x10428dc84 0x104338a08 0x104025f94 0x1043d8e10 0x10402424c 0x10005fbe8 0x1813268b8)
libc++abi.dylib: terminating with uncaught exception of type NSException

There is no meaningful stack trace and I have been unable to decipher what the addresses in “First throw call stack” mean. Setting breakpoint in Xcode on NSObject’s “doesNotRecognizeSelector” (which gets triggered during this crash) revealed, that the stack trace just leads to “UIKit.UIApplicationMain()” invocation in my “main()”, which is probably wrong.

I have not found any place in my code, which could trigger this error. The method in question (Long.intValue()) is there, loaded and works, I have added it explicitly to rule out that it is removed from the binary, as it happens sometimes with proguard (I am not even sure if MOE uses proguard).

So the question is:
Has anybody encountered something like this before? I have exhausted my options - what do I do now? Are there any MOE debug flags that enable verbose logging or something that could help me to debug this?

So after more debugging, I have narrowed it down to following code, which should set the UIButton’s title to given text but underscored.

    final NSAttributedString underlinedTitle = NSAttributedString.alloc().initWithStringAttributes(
            LocalizationUtil.localize(title),
            (NSDictionary<String, ?>) NSDictionary.dictionaryWithObjectForKey(NSUnderlineStyle.StyleSingle, NSUnderlineStyleAttributeName())
    );
    button.setAttributedTitleForState(underlinedTitle, UIControlState.Normal);

I have no idea why it doesn’t work, but I will figure that out, maybe I am using the API incorrectly.

What caused my problem was that the crash happened somewhere inside the UIKit and not directly in the Java code, which obscured the cause. However I’d still like to know what could I do to figure this out earlier. Is it a bug in/unfinished part of MOE that such situations result in so cryptic errors?

Hi, from the log snippet it looks like that you tried to call java.lang.Long.intValue on an Objective-C object as an Objective-C selector. I am not sure why this would happen without looking at your code.

Please try to install an uncaught exception handler into the MOE VM, and see if it provides some more context.

Thanks for the reply!
I have already figured out what code caused it, see above.

But for the future and for others: How do I install uncaught exception handler into MOE VM? Is it through the standard “Thread.setDefaultUncaughtExceptionHandler()” or through something else?

So I have finally figured out how to do what I wanted (basically add underline to NSAttributedString). It appears that MOE’s handling of objc enums in places that accept generic Object/id is buggy/incorrect. (Should I open an issue somewhere?)

The solution I made is available here, for anybody facing a similar problem.

1 Like