【发布时间】:2015-11-18 01:07:01
【问题描述】:
我的二进制文件因以下几行而崩溃
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 -
问题中的错字,对不起
标签: android c++ exception fstream libc++