Issue
I'm a beginner on Android NDK, and less aware of C++. I have written some logic using C++ and then build them to .so file, then use it with JNI for my Android project, but sometimes it will raise crash in .so files.
I want to quickly fix this problem, I want to know if I can use try-catch syntax to my C++ code? (I tried, but it always reports compile error.) If yes, how can I do it?
C++ codes:
JNIEXPORT void JNICALL Java_xxoo_com_xxoo_model_ARDrawModel_draw(JNIEnv * env, jobject obj) {
try {
if(controller)
{
controller->Update(/*float dt*/);
controller->DrawGLScene();
}
} catch (int a) {
LOGD("drawModel---exception");
}
}
Compile error: error: exception handling disabled, use -fexceptions to enable
Solution
Yes. you can use try catch in c++ NDK code, but it will not catch all native crashes.
And as your error says, you need to explicitly enable exceptions feature while compiling NDK code. To do so add
APP_CPPFLAGS += -fexceptions
to Application.mk file.
If you want to catch all possible NDK crashes check out https://github.com/xroche/coffeecatch library.
Answered By - V-master
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.