gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:33:31 29 4
gpt4 key购买 nike

注意 这个问题现在已经解决了 - 请看下面我的回答 - 除非有人删除这篇文章,否则它会留在这里只是为了记录我自己的愚蠢,我想......仍然,使 C++ 异常在 Android JNI 代码中工作的所有步骤和配置在下面都是正确的,所以也许它偶尔会对某人有所帮助 - Greg

我正在阅读有关此事的所有可能 Material ,但无法在静态构建的 native 库中使我的 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()。现在看看代码,还有我的堆栈跟踪,这很明显,再次为我的笨拙道歉!

格雷格

关于android - Android JNI 代码中的 C++ 异常 - 再次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34757133/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com