Search code examples
androidc++exceptionjava-native-interfacetry-catch

C++ exceptions in Android JNI code - again


Note this issue is now resolved - see my answer below - and unless someone deletes this post, it will stay here only to document only my own stupidity, I guess... Still, all the steps and config to make C++ exceptions work in Android JNI code are correct below, so maybe it will help someone occasionally - Greg

I'm reading all possible material on this matter and cannot make my C++ exceptions in a statically built native library work... Any idea what I'm missing?

Application.mk, as you can see below I tried both gnustl_static and stlport_static, nothing works. I even forced a rebuild of stlport_static...

NDK_TOOLCHAIN_VERSION= 4.9
APP_PLATFORM := android-10
#APP_STL := gnustl_static
APP_STL := stlport_static
APP_CPPFLAGS += -fexceptions
APP_CPPFLAGS += -frtti
# STLPORT_FORCE_REBUILD := true
ifeq ($(NDK_DEBUG),1)
    APP_OPTIM := debug
    APP_ABI := x86
else 
    APP_OPTIM := release
    APP_ABI := mips armeabi armeabi-v7a x86
endif

The code I'm trying:

//-----------------------------------------------------------------------
// Generic external error exception
struct ExternalException : public std::exception
{
public:
    explicit ExternalException(const std::string &what)
        : message(what) {}
    virtual ~ExternalException() throw() {}
    const char      *what() const throw() { return message.c_str(); }
protected:
    const std::string message;
};

then elsewhere:

try
{
    ret = SomeFunction();
}
catch (...)
{
    ret = -1;
}

I also tried above catch (ExternalException& e) {...}, also does not help.

SomeFunction() calls at a certain error condition:

throw ExternalException(what);

Then the app simply crashes. When I analyze stack trace, I see that the exception Unwind...() call does nothing and then it falls through to a terminate function.

I feel like banging my head against a wall... What's wrong here???

Just for a quick reference, here are my local compiler options:

LOCAL_CFLAGS += -Wno-write-strings -gdwarf-2
LOCAL_CFLAGS += -ffunction-sections -fdata-sections -fvisibility=hidden
LOCAL_CPPFLAGS += -ffunction-sections -fdata-sections -fvisibility=hidden

Update

Here is part of the stack trace captured, this was with armeabi-v7a build on a Nexus 6P device. Previously I tested debug builds on x86 emulator, exactly the same problem. Will try to test with different C/CPP flags next, maybe there is a conflict there?

********** Crash dump: **********
Build fingerprint: 'google/angler/angler:6.0.1/MMB29P/2473553:user/release-keys'
pid: 12307, tid: 12352, name: AsyncTask #4  >>> com.hyperionics.avar <<<
signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
Stack frame     #00 pc 00042374  /system/lib/libc.so (tgkill+12)
Stack frame     #01 pc 0003ff81  /system/lib/libc.so (pthread_kill+32)
Stack frame     #02 pc 0001c73f  /system/lib/libc.so (raise+10)
Stack frame     #03 pc 000198f1  /system/lib/libc.so (__libc_android_abort+34)
Stack frame     #04 pc 000174b0  /system/lib/libc.so (abort+4)
Stack frame     #05 pc 002ac143  /data/app/com.hyperionics.avar-1/lib/arm/libHyperionics_avar.so: Routine __gabixx::__default_terminate() at /tmp/ndk-user/tmp/build-stlport/ndk/sources/cxx-stl/gabi++/src/terminate.cc:75
Stack frame     #06 pc 002ac14d  /data/app/com.hyperionics.avar-1/lib/arm/libHyperionics_avar.so: Routine __gabixx::__terminate(void (*)()) at /tmp/ndk-user/tmp/build-stlport/ndk/sources/cxx-stl/gabi++/src/terminate.cc:84
Stack frame     #07 pc 002ac1a5  /data/app/com.hyperionics.avar-1/lib/arm/libHyperionics_avar.so: Routine std::terminate() at /tmp/ndk-user/tmp/build-stlport/ndk/sources/cxx-stl/gabi++/src/terminate.cc:110
Stack frame     #08 pc 002ab80f  /data/app/com.hyperionics.avar-1/lib/arm/libHyperionics_avar.so: Routine __cxxabiv1::call_terminate(_Unwind_Control_Block*) at /tmp/ndk-user/tmp/build-stlport/ndk/sources/cxx-stl/gabi++/src/helper_func_internal.cc:54
Stack frame     #09 pc 002ab21d  /data/app/com.hyperionics.avar-1/lib/arm/libHyperionics_avar.so: Routine throwException at /tmp/ndk-user/tmp/build-stlport/ndk/sources/cxx-stl/gabi++/src/cxxabi.cc:271
Stack frame     #10 pc 002ab2e1  /data/app/com.hyperionics.avar-1/lib/arm/libHyperionics_avar.so: Routine __cxa_throw at /tmp/ndk-user/tmp/build-stlport/ndk/sources/cxx-stl/gabi++/src/cxxabi.cc:335
Stack frame     #11 pc 00045e58  /data/app/com.hyperionics.avar-1/lib/arm/libHyperionics_avar.so: Routine IOError(std::string const&, std::string const&) at C:\android\TtsNativeLib/jni/mylib/error.h:128 (discriminator 4)

Greg


Solution

  • Apologies, all works right. It was my programming error, apparently working at the evenings I'm not in my best.

    Briefly, what was the problem: in my call to the SomeFunction() I actually had parameters, and the exception was in creation of these parameters, not inside the function. Simply my 'try {' instruction had to be move up... It was like:

    Ptr<OutPackedStream> pout = CreatePackedStream(fname.c_str());
    try
    {
        ret = SomeFunction(pout);
    }
    catch (...)
    {
        ret = -1;
    }
    

    So obviously I couldn't catch an exception in CreatePackedStream(), because it was not inside try{}... What confused me though was that the stack trace was pointing at ret = SomeFunction(pout) line, only then went to CreatePackedStream(). Now looking at the code, and also my stack traces this is so obvious, sorry again for my clumsiness!

    Greg