Android JNI 代码中的 C++ 异常 - 再次
C++ exceptions in Android JNI code - again
注意 这个问题现在已经解决了 - 请看下面我的回答 - 除非有人删除这个 post,否则它只会留在这里作为文档只是我自己的愚蠢,我想......不过,在 Android JNI 代码中使 C++ 异常工作的所有步骤和配置在下面都是正确的,所以也许它偶尔会对某人有所帮助 - Greg
我正在阅读关于此事的所有可能 material,但无法在静态构建的本机库中使我的 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)
格雷格
抱歉,一切正常。这是我的编程错误,显然我在晚上工作时状态不佳。
简而言之,问题是什么:在我对 SomeFunction() 的调用中,我实际上有参数,异常是在创建这些参数时,而不是在函数内部。只是我的 'try {' 指令必须向上移动...就像:
Ptr<OutPackedStream> pout = CreatePackedStream(fname.c_str());
try
{
ret = SomeFunction(pout);
}
catch (...)
{
ret = -1;
}
所以很明显我无法在 CreatePackedStream() 中捕捉到异常,因为它不在 try{} 中...不过让我感到困惑的是堆栈跟踪仅指向 ret = SomeFunction(pout)
行然后去了CreatePackedStream()
。现在看看代码,还有我的堆栈跟踪,这很明显,再次为我的笨拙道歉!
格雷格
注意 这个问题现在已经解决了 - 请看下面我的回答 - 除非有人删除这个 post,否则它只会留在这里作为文档只是我自己的愚蠢,我想......不过,在 Android JNI 代码中使 C++ 异常工作的所有步骤和配置在下面都是正确的,所以也许它偶尔会对某人有所帮助 - Greg
我正在阅读关于此事的所有可能 material,但无法在静态构建的本机库中使我的 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)
格雷格
抱歉,一切正常。这是我的编程错误,显然我在晚上工作时状态不佳。
简而言之,问题是什么:在我对 SomeFunction() 的调用中,我实际上有参数,异常是在创建这些参数时,而不是在函数内部。只是我的 'try {' 指令必须向上移动...就像:
Ptr<OutPackedStream> pout = CreatePackedStream(fname.c_str());
try
{
ret = SomeFunction(pout);
}
catch (...)
{
ret = -1;
}
所以很明显我无法在 CreatePackedStream() 中捕捉到异常,因为它不在 try{} 中...不过让我感到困惑的是堆栈跟踪仅指向 ret = SomeFunction(pout)
行然后去了CreatePackedStream()
。现在看看代码,还有我的堆栈跟踪,这很明显,再次为我的笨拙道歉!
格雷格