简体   繁体   中英

Android C++ exception throw causes SIGSEGV

I ran into some problems while integrating C++ code in my Android application. I used Djinni library to generate the JNI code that I'm using in the application. I am using CMake to build my C++ library and I'm linking OpenCV library (with .so files) in the process. I use the NDK r19.

I have a C++ class integrated to the project that can throw an exception, an std::runtime_error , but this exception has a behavior that I don't understand.

First problem :

I looked into the doc here https://developer.android.com/ndk/guides/cmake and used the attribute -DANDROID_CPP_FEATURES=rtti exceptions to enable C++ exceptions. With that, as soon as my exception is thrown in the C++, I get a SIGABRT which makes my app crash. This happens even if the throw in the C++ is surrounded with a catch(...) block. I also tried to apply the flags -fexceptions and -frtti through cppFlags but with no success.

Second problem :

I used another STL with the attribute -DANDROID_STL and put it to c++_shared . With this attribute, my exceptions are correctly handled on some architectures. But on the architecture armv7l (found using System.getProperty("os.arch") ), the throw causes a SIGSEGV which makes my app crash.

Here the android section of my build.gradle file :

android {
    compileSdkVersion Versions.COMPILE_SDK
    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion Versions.MIN_SDK
        targetSdkVersion Versions.TARGET_SDK
        versionCode 1
        versionName "1.0"
        externalNativeBuild {
            cmake {
                cppFlags "-v"
                arguments "-DANDROID_CPP_FEATURES=rtti exceptions", "-DANDROID_STL=c++_shared"
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
    sourceSets {
        main.jniLibs.srcDirs += 'src/main/jniLibs'
    }
}

Here is my CMakeLists.txt file :

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Library name
set(library_name native-lib)

# Path to djinni support code
set(support_dir src/djinni/support-lib/jni)
# Path to generated code and own c++ implementation
set(include_dirs src/djinni/generated/jni src/djinni/generated/cpp src/main/cpp)

# Djinni support code that needs to be compiled
file(
        GLOB_RECURSE support_srcs
        ${support_dir}/*.cpp)

# Generated code and c++ implementations that needs to be compiled
file(
        GLOB_RECURSE lib_srcs
        src/djinni/generated/cpp/*.cpp
        src/djinni/generated/jni/*.cpp
        src/main/cpp/*.cpp)


# All the implementation files that make up our library
set(complete_srcs ${support_srcs} ${lib_srcs})

# Define library referring to the sources above
add_library(${library_name} SHARED ${complete_srcs})


# Add OpenCV library
set(opencv_library_name opencv)
set(opencv_dir src/main/jniLibs/${CMAKE_ANDROID_ARCH_ABI})

add_library(${opencv_library_name} SHARED IMPORTED)
set_target_properties(${opencv_library_name} PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/${opencv_dir}/libopencv_java3.so)

# We link opencv to our datamatrix library
target_link_libraries(${library_name} ${opencv_library_name})

# Define INCLUDE DIRECTORIES property for native-lib
target_include_directories(${library_name} PUBLIC ${include_dirs} ${support_dir})

Here is the command building a single C++ file during the build process of my application (found using -v in cppFlags ) :

 "/Users/me/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++" -cc1 -triple i686-none-linux-android21 -emit-obj -mrelax-all -mnoexecstack -disable-free -disable-llvm-verifier -discard-value-names -main-file-name usercodedecoder.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu i686 -target-feature +ssse3 -dwarf-column-info -debug-info-kind=standalone -dwarf-version=4 -debugger-tuning=gdb -target-linker-version 241.9 -v -ffunction-sections -fdata-sections -coverage-notes-file /Users/me/Documents/workspace/my_project/app/.externalNativeBuild/cmake/debug/x86/CMakeFiles/native-lib.dir/src/main/cpp/usercodedecoder.cpp.gcno -resource-dir /Users/me/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/lib64/clang/8.0.2 -dependency-file CMakeFiles/native-lib.dir/src/main/cpp/usercodedecoder.cpp.o.d -sys-header-deps -MT CMakeFiles/native-lib.dir/src/main/cpp/usercodedecoder.cpp.o -D datamatrix_native_lib_EXPORTS -I ../../../../src/djinni/generated/jni -I ../../../../src/djinni/generated/cpp -I ../../../../src/main/cpp -I ../../../../src/djinni/support-lib/jni -D ANDROID -isysroot /Users/me/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/sysroot -internal-isystem /Users/me/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/c++/v1 -internal-isystem /Users/me/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/local/include -internal-isystem /Users/me/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/lib64/clang/8.0.2/include -internal-externc-isystem /Users/me/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/i686-linux-android -internal-externc-isystem /Users/me/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/include -internal-externc-isystem /Users/me/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include -O0 -Wformat -Werror=format-security -fdeprecated-macro -fdebug-compilation-dir /Users/me/Documents/workspace/my_project/app/.externalNativeBuild/cmake/debug/x86 -ferror-limit 19 -fmessage-length 0 -stack-protector 2 -mstackrealign -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -o CMakeFiles/native-lib.dir/src/main/cpp/usercodedecoder.cpp.o -x c++ ../../../../src/main/cpp/usercodedecoder.cpp

I did some research about this kind of problems but couldn't find something that has helped me remove this SIGSEGV error on some architectures.

I suppose the problem is in my build process, but I can't figure out what to do to resolve it.

opencv was probably built incorrectly. See https://github.com/android-ndk/ndk/issues/889 for instructions on diagnosing that. Just do the same for your opencv library as that bug suggests for qt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM