我的二进制文件在以下代码行崩溃:
I DEBUG : pid: 1190, tid: 1367, name: Binder_1 >>> /system/bin/fingerprint_proxy <<<
I DEBUG : signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
I DEBUG : Abort message: 'terminating with uncaught exception of type std::__1::ios_base::failure: ios_base::clear: unspecified iostream_category error'
引起异常的代码如下所示。
try {
std::ofstream out;
out.exceptions( std::ofstream::failbit | std::ofstream::badbit );
out.open("bad_path");
} catch(std::ios_base::failure &e) {
// skipped
}
由于某些原因,异常没有被捕获,所以我添加了更多的捕获块。
try {
std::ofstream out;
out.exceptions( std::ofstream::failbit | std::ofstream::badbit );
out.open("bad_path");
} catch(std::ios_base::failure &e) {
// skipped
} catch(std::__1::ios_base::failure &e) {
// skipped
} catch(std::exception &e) {
// skipped
} catch(...) {
// caught
}
这次只有最后一个catch块捕获了异常,为了确保这不是继承问题,我添加了代码。
try {
throw std::ios_base::failure("miau");
} catch(std::exception &e) {
// caught
}
我的 Android.mk 文件看起来像这样:
include $(CLEAR_VARS)
LOCAL_CFLAGS := -Wall
LOCAL_CPPFLAGS := -Wno-write-strings -Wall -std=c++11 -fexceptions
LOCAL_SRC_FILES := test.cpp
LOCAL_MODULE := test_app
LOCAL_MODULE_TAGS := optional
include external/libcxx/libcxx.mk
include $(BUILD_EXECUTABLE)
我从包含在“include external/libcxx/libcxx.mk”中的共享库libc++.so获得了std::命名空间和异常实现。 因此,我静态编译了libc++.so。
include $(CLEAR_VARS)
LOCAL_CFLAGS := -Wall
LOCAL_CPPFLAGS := -Wno-write-strings -Wall -std=c++11 -fexceptions
LOCAL_SRC_FILES := test.cpp
LOCAL_MODULE := test_app
LOCAL_MODULE_TAGS := optional
LOCAL_SHARED_LIBRARIES := libc++
include external/libcxx/libcxx.mk
include $(BUILD_EXECUTABLE)
它起作用了!
异常通过std::exception子句被捕获,尽管它将二进制大小乘以8,所以这不是一个解决方案。
因此,似乎共享库抛出的异常无法被捕获。
有人有什么想法吗?
这是您方便使用的要点https://gist.github.com/amfern/2377e9a3cd1ccc0186ea
std::exception
,而不是std::exceptions
。 - NathanOliver