我知道,这个问题已经问了很多次了,但是我找不到解决方案。

我有以下情况:

   A
  / \
 /   \
B <-- C
  • A是一个共享库,其中包含类EException
  • B和C链接到A
  • C也是
  • 的共享库
  • B在运行时动态加载C

  • 在某个时候C抛出EException的实例:
    void doSometing() {
        throw EException("test-message");
    }
    

    B中,我想捕获此异常:
    try {
        doSomething();
    } catch (const EException& ex) {
        // Not reached
    } catch (...) {
        // Not reached
    }
    

    但是正如代码中提到的,没有任何catch子句被调用。相反,执行此代码的线程被中止。

    我尝试了以下操作:
  • 编译
  • 时,EException的可见性属性设置为“默认”
  • EException头文件仅包含声明
  • 我在A,B和C中使用链接器选项-fvisibility=hidden
  • 我在C
  • 中使用链接器选项-E
    使用nm我得到A:
    0000000000066260 T EException::EException(QString const&)
    0000000000066306 T EException::EException(EException const&)
    00000000000661d0 T EException::EException()
    0000000000066260 T EException::EException(QString const&)
    0000000000066306 T EException::EException(EException const&)
    00000000000661d0 T EException::EException()
    00000000000664de T EException::~EException()
    000000000006641e T EException::~EException()
    000000000006641e T EException::~EException()
    00000000000663b6 T EException::operator=(EException const&)
    <...>
    000000000028de40 V typeinfo for EException
    000000000028dd80 V typeinfo for EException*
    000000000007342b V typeinfo name for EException
    0000000000072ab7 V typeinfo name for EException*
    000000000028de00 V vtable for EException
    

    对于B:
    U EException::EException(QString const&)
    U EException::~EException()
    <...>
    0000000000726f60 V typeinfo for EException
    

    C:
    U EException::EException(QString const&)
    U EException::~EException()
    <...>
    U typeinfo for EException
    

    问题可能是B使用自己的EException typeinfo,而C使用A提供的typeinfo吗?我该如何解决?

    我的环境:

    x86_64-linux-gnu上的
  • gcc 4.6.3
  • 使用Qt 的


  • 感谢您的帮助!

    最佳答案

    对于gcc
    如前所述,EException的vtable(包含typeinfo对象的条目)似乎在某些翻译单元中重复,这绝对是gcc EException中定义一个虚拟的脱机析构函数(必须是 header 中的第一个虚拟函数声明)来固定A的vtable,对我来说很成功。

    EException发布完整的头文件也可能会有所帮助。

    关于c++ - 跨二进制边界的异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10787492/

  • 10-12 20:53