如何在作为共享库加载时正确关闭Qt?

时间:2012-03-21 13:22:56

标签: qt lua sigsegv

我有一些与Qt相关的Lua绑定,可以在mac os x(Qt 4.8.0)上找到,但在Ubuntu linux上崩溃(Qt 4.7.4)。 Qt代码加载了一个dlopen,然后使用app-> exec()将执行传递给Qt。

Lua代码:

require 'mimas' -- Load shared library mimas.so linked to Qt libs
app = mimas.Application() -- Just a wrapper around QApplication
app:exec()
-- In some callback: app:quit()

当Lua退出时(在app:exec()返回之后),它在'mimas'共享库上执行dlclose,最终在SIGSEGV中。回溯:

Program received signal SIGSEGV, Segmentation fault.
QList<QFactoryLoader*>::removeAll (this=0x0, _t=@0xbfffebac)
    at ../../include/QtCore/../../src/corelib/tools/qlist.h:760
760 ../../include/QtCore/../../src/corelib/tools/qlist.h: No such file or directory.
    in ../../include/QtCore/../../src/corelib/tools/qlist.h
(gdb) bt
#0  QList<QFactoryLoader*>::removeAll (this=0x0, _t=@0xbfffebac)
    at ../../include/QtCore/../../src/corelib/tools/qlist.h:760
#1  0x0131126c in QFactoryLoader::~QFactoryLoader (this=0x8104a48, 
    __in_chrg=<optimized out>) at plugin/qfactoryloader.cpp:208
#2  0x01311302 in QFactoryLoader::~QFactoryLoader (this=0x8104a48, 
    __in_chrg=<optimized out>) at plugin/qfactoryloader.cpp:209
#3  0x009143a8 in QGlobalStaticDeleter<QFactoryLoader>::~QGlobalStaticDeleter (
    this=0x11c3200, __in_chrg=<optimized out>)
    at ../../include/QtCore/../../src/corelib/global/qglobal.h:1825
#4  0x001e7d2b in __cxa_finalize () from /lib/i386-linux-gnu/libc.so.6
#5  0x00842a94 in __do_global_dtors_aux ()
   from /usr/lib/i386-linux-gnu/libQtGui.so.4
[snip]
#13 0x0016bd28 in dlclose () from /lib/i386-linux-gnu/libdl.so.2

如何正确关闭Qt以使其不会以此内存错误结束?

问题似乎是qt_factory_loaders()返回NULL。我将尝试使用Qt 4.8.0来查看是否有变化。

[编辑]我可以缩小问题范围(与Lua无关)并填写bug report到qt。

2 个答案:

答案 0 :(得分:0)

不完全确定您的问题是什么,但您可以明确告诉Qt应用程序退出app:quit()

答案 1 :(得分:0)

dlclose()可能是你想要的吗?

http://www.kernel.org/doc/man-pages/online/pages/man3/dlsym.3.html

   The function dlclose() decrements the reference count on the dynamic library
   handle handle.  If the reference count drops to zero and no other loaded
   libraries use symbols in it, then the dynamic library is unloaded.

   The function dlclose() returns 0 on success, and nonzero on error.

例如:

#ifdef MAC_OS
    // unload library for MAC
#else //if linux
    dlclose(lib_handle);
#endif