小编Säm*_*ämy的帖子

二进制边界的例外

我知道,这个问题已被问过很多次,但我无法找到解决问题的方法.

我有以下情况:

   A
  / \
 /   \
B <-- C
Run Code Online (Sandbox Code Playgroud)
  • A是包含该类的共享库 EException
  • B和C链接A
  • C也是一个共享库
  • B在运行时动态加载C.

在某些时候,C抛出一个实例EException:

void doSometing() {
    throw EException("test-message");
}
Run Code Online (Sandbox Code Playgroud)

B我想抓住这个例外:

try {
    doSomething();
} catch (const EException& ex) {
    // Not reached
} catch (...) {
    // Not reached
}
Run Code Online (Sandbox Code Playgroud)

但正如代码中所提到的,没有一个catch子句被调用.相反,执行此代码的线程将被中止.

我尝试了以下事项:

  • EException编译A时,visibility属性设置为"default"
  • EException头文件只包含声明
  • -fvisibility=hidden在A,B和C中使用链接器选项
  • -E在C中使用链接器选项

使用nm我得到A:

0000000000066260 T EException::EException(QString const&)
0000000000066306 T EException::EException(EException const&)
00000000000661d0 T EException::EException() 
0000000000066260 T EException::EException(QString const&) …
Run Code Online (Sandbox Code Playgroud)

c++ exception-handling g++ shared-libraries

21
推荐指数
1
解决办法
1712
查看次数

为什么我会收到符号查找错误?

我正在编写一个库,它使用dlsym在我的主应用程序中动态加载.我有以下文件:

library.h

#include <ilibrary.h>
#include <igateway.h>

class LibraryImpl;

class Library: public ILibrary {
public:
    static ILibrary* instance();

    IGateway* getGateway() const;

protected:
    Library();
    virtual ~Library();

private:
    static ILibrary* instance_;
    LibraryImpl* library_;
};

extern "C" {
    IMPORT_EXPORT ILibrary* getLibrary();
}
Run Code Online (Sandbox Code Playgroud)

library.cpp

#include "library.h"

#include "business/BCGateway.h"


class LibraryImpl {
public:
    IGateway* getGateway();
};

IGateway* LibraryImpl::getGateway() {
    return BCGateway::instance();
}



ILibrary* Library::instance_ = NULL;
ILibrary* Library::instance() {
    return instance_ ? instance_ : (instance_ = new Library);
}

Library::Library() {
    library_ = new LibraryImpl();
} …
Run Code Online (Sandbox Code Playgroud)

c++

7
推荐指数
1
解决办法
3万
查看次数

为什么我的应用程序有时会在SIGSEGV关闭时崩溃?

我用c ++编写了一个使用Qt 4.7.4的应用程序.启动时,它会加载一些自动编写的动态库,这些库也使用Qt(如果这在某种程度上有用).

关闭应用程序时,有时它会与SIGSEGV崩溃并发生以下回溯:

#0  malloc_consolidate (av=0x7ffff58b21c0) at malloc.c:5155
#1  0x00007ffff5591659 in malloc_consolidate (av=0x7ffff58b21c0) at malloc.c:5115
#2  _int_free (av=0x7ffff58b21c0, p=<optimized out>) at malloc.c:5034
#3  0x00007ffff5594d7c in __GI___libc_free (mem=<optimized out>) at malloc.c:3738
#4  0x00007ffff5e9e0ac in QString::free (d=0xdb0290) at tools/qstring.cpp:1186
#5  0x00007fffe60b3d83 in ~QString (this=0xba0328, __in_chrg=<optimized out>) at ../../include/QtCore/../../src/corelib/tools/qstring.h:883
#6  node_destruct (to=0xba0328, from=0xba0328, this=<optimized out>) at ../../include/QtCore/../../src/corelib/tools/qlist.h:420
#7  QList<QString>::free (data=0xba0310, this=<optimized out>) at ../../include/QtCore/../../src/corelib/tools/qlist.h:744
#8  0x00007fffe60c2b49 in ~QList (this=0xba0388, __in_chrg=<optimized out>) at ../../include/QtCore/../../src/corelib/tools/qlist.h:719
#9  ~QStringList (this=0xba0388, __in_chrg=<optimized out>) at ../../include/QtCore/../../src/corelib/tools/qstringlist.h:66
#10 ~SignalHook …
Run Code Online (Sandbox Code Playgroud)

c++ qt segmentation-fault

6
推荐指数
1
解决办法
7741
查看次数

dynamic_cast返回NULL,但不应

我具有以下类层次结构:

class IStorage {
    [...]
}
Q_DECLARE_INTERFACE(IStorage, "ch.gorrion.smssender.IStorage/1.0")


class ISQLiteStorage: public IStorage { 
    Q_INTERFACES(IStorage)

    [...] 
}
Q_DECLARE_INTERFACE(ISQLiteStorage, "ch.gorrion.smssender.ISQLiteStorage/1.0")


class DASQLiteStorage: public QObject, public ISQLiteStorage {
    Q_OBJECT
    Q_INTERFACES(ISQLiteStorage)

    [...]
}
Run Code Online (Sandbox Code Playgroud)

我正在使用QT,并尝试使用QtPlugin创建一个插件(针对我的应用)。我正在创建DASQLiteStorage的实例,并将此实例提供给插件中的FROM对象:

// the next line is within my main app.
// storage is the DASQLiteStorage instance.
// gateway is an object from within the plugin.
gateway->setDefaultStorage(storage);

// this method lies within the plugin
void AbstractGateway::setDefaultStorage(IStorage* storage) {
    defaultStorage_ = dynamic_cast<ISQLiteStorage*>(storage);
}
Run Code Online (Sandbox Code Playgroud)

问题是,dynamic_cast返回空指针(不是预期的),而在我的主应用程序中执行dynamic_cast时(即在“ gateway-> setDefaultStorage(storage);”之前)给了我有效的指针(预期)。

有谁知道为什么会这样?程序与插件在不同的内存范围内运行吗?这会导致此类问题吗?任何想法如何解决这一问题?

非常感谢!


编辑:我已经尝试了一些建议:

// this method lies …
Run Code Online (Sandbox Code Playgroud)

c++ plugins qt dynamic-cast

5
推荐指数
1
解决办法
6450
查看次数