Android共享库中未捕获的异常

3

我的二进制文件在以下代码行崩溃:

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
抱歉,问题中有错别字。 - amfern
1个回答

0

将 -frtti 添加到 LOCAL_CPPFLAGS 中解决了问题。

因为异常在内部使用 RTTI,不指定 -frtti 标志会导致两个表格,一个来自 libc++,另一个来自我的二进制文件。 底层异常

注意:由于所有 Android 本地库都是没有使用 RTTI 编译的,这使得它们无法包含在 -frtti 编译的二进制文件/库中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接