You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Repository navigation
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Catch C++ exceptions before returning to JNI #2825
Per #2804, the Android SDK suffers from a native crash any time there’s an uncaught C++ exception at the mbgl level. An uncaught exception there usually indicates developer error, so the Android SDK needs to present that error in a way that’s actionable without spelunking into mbgl code. The Android bindings cannot rely on an assumption that the C++ code won’t throw exceptions.
platform/android/jni.cpp needs to have C-style exception handling. For example, nativeSetStyleURL() should be wrapped in a try-catch statement. When an exception is caught, this function should return some kind of error code. For functions that already need to return a value, this would imply some kind of out parameter or a tuple-typed return value.
Though this could be a fair amount of work, it pays off in the form of Android bindings that can actually be debugged.
As discussed in slack a wrinkle with nativeSetStyleURL is that the invalid style URL error will be thrown from the map thread, not the UI thread, so adding a catch to nativeSetStyleURL will not work.
Instead we can take advantage of how Java checking JNI thrown Java exceptions. If we convert exception to Java exception in the map thread instead of killing the thread. Then when we next call into Java land (View::invalidate) the exception will be picked up and crash the app Java side with a useful message.
We also need to log the C++ exception stack trace to logcat first so that in the log we have both the original stack trace, and the new Java one which will point to invalidate()
Activity
ljbade commentedon Oct 27, 2015
As discussed in slack a wrinkle with
nativeSetStyleURLis that the invalid style URL error will be thrown from the map thread, not the UI thread, so adding a catch tonativeSetStyleURLwill not work.Instead we can take advantage of how Java checking JNI thrown Java exceptions. If we convert exception to Java exception in the map thread instead of killing the thread. Then when we next call into Java land (
View::invalidate) the exception will be picked up and crash the app Java side with a useful message.We also need to log the C++ exception stack trace to logcat first so that in the log we have both the original stack trace, and the new Java one which will point to
invalidate()ljbade commentedon Oct 27, 2015
@1ec5 Historic context on my previous attempt at C++ exception handling:
#562
ljbade commentedon Oct 28, 2015
@1ec5 Should we also open a iOS ticket for iOS exception handling? Will help in #2775 (comment)
1ec5 commentedon Oct 28, 2015
@ljbade, once #2762 is fixed, iOS will get such notifications for free.
bleege commentedon Oct 28, 2015
The joys of living in an all C++ world. 😃