【问题标题】:android exceptions not caught from shared lib未从共享库中捕获的 android 异常
【发布时间】: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++


【解决方案1】:

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

因为异常在内部使用 RTTI,所以不指定 -frtti 标志将导致两个表,一个来自 libc++,另一个用于我的二进制文件。 exceptions under the hood

注意:由于所有 android 原生库都是在没有 RTTI 的情况下编译的,因此无法将它们包含在 -frtti 编译的二进制/库中。

【讨论】:

    猜你喜欢
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-26
    • 2012-06-09
    • 2011-04-07
    相关资源
    最近更新 更多