Android JNI代码中的C ++异常 - 再次

时间:2016-01-13 01:55:20

标签: android c++ exception java-native-interface try-catch

注意 此问题现已解决 - 请参阅下面的答案 - 除非有人删除此帖子,否则它只会记录我自己的愚蠢,我猜...仍然,使用C ++异常在Android JNI代码中工作的所有步骤和配置在下面都是正确的,所以它可能会偶尔帮助某人--Greg

我正在阅读有关此问题的所有可能材料,并且无法在静态构建的本机库中使我的C ++异常工作......任何想法我缺少什么?

Application.mk,如下所示,我尝试了gnustl_static和stlport_static,没有任何效果。我甚至强迫重建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

我正在尝试的代码:

//-----------------------------------------------------------------------
// 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;
};

然后在其他地方:

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

我也在catch (ExternalException& e) {...}之上尝试过,也没有帮助。

SomeFunction()调用某个错误条件:

throw ExternalException(what);

然后应用程序崩溃了。当我分析堆栈跟踪时,我发现异常 Unwind ...()调用什么都不做,然后它会转到终止函数。

我觉得我的头撞在墙上......这里有什么问题???

仅供快速参考,以下是我的本地编译器选项:

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

更新

以下是捕获的堆栈跟踪的一部分,这是在Nexus 6P设备上构建的armeabi-v7a。以前我在x86模拟器上测试了调试版本,完全相同的问题。接下来会尝试用不同的C / CPP标志进行测试,也许那里存在冲突?

********** 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)

格雷格

1 个答案:

答案 0 :(得分:1)

道歉,一切正常。这是我的编程错误,显然是在晚上工作,我不是最好的。

简单地说,问题是什么:在我对SomeFunction()的调用中,我实际上有参数,而异常是在创建这些参数时,而不是在函数内部。只是我的尝试{&#39;指示必须向上移动......就像:

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

所以很明显我无法在CreatePackedStream()中捕获异常,因为它不在try {}内部......但令我困惑的是堆栈跟踪指向ret = SomeFunction(pout)行,仅然后去了CreatePackedStream()。现在查看代码,以及我的堆栈跟踪,这是非常明显的,再次抱歉我的笨拙!

格雷格

相关问题