Segmentation Fault with Q_Object when used in a static library [Linux]
-
I am currently trying to build a GUI project linking to a static library. I'm working in Centos8 stream with g++ (GCC) 8.5.0 20210514 (Red Hat 8.5.0-10), c++17 and Qt5.15.2. My program throws a segmentation fault before main at run time when I try to use any class in the static library inherited to a Qt Object and has Q_Object marco. If I comment out Q_Object and it run perfectly fine. If I move this class out from the static library and place it in the main GUI project, it also runs fine even with Q_Object. An example code is as follows:
mywindow.h
#include <QMainWindow> class MyWindow : public QMainWindow { Q_OBJECT // comment out this will resolve segmentation fault public: MyWindow(); ~MyWindow(); private: void InitWindow(); };
mywindow.cpp
#include <QHBoxLayout> MyWindow::MyWindow() { InitWindow(); } MyWindow::~MyWindow() {} void MyWindow::InitWindow() { QWidget* window = new QWidget; QHBoxLayout* mainLayout = new QHBoxLayout; window->setLayout(mainLayout); setCentralWidget(window); }
main.cpp
int main(int argc, char* argv[]) { QApplication app(argc, argv); app.setApplicationName("LibAudio Demo App"); MyWindow myapp; myapp.show(); return app.exec(); }
So if mywindow.h and mywindow.cpp are in a static library, the program crashed with segmentation fault before main. If I move them to the main project, it runs fine. Commenting out Q_Object also solves the issue. However I do want Q_Object for signal and slots implementation.
Anyone has any idea why this is happening and how to resolve this? (e.g. I miss any compile option when creating static library?)
Update 1:
I removed everything else in my static library and just have mywindows.h/cpp files. Surprisingly it runs fines. So how can Q_Object macro gets affected by other implementation?Update 2:
gdb suggests that the seg fault happens at QString::arg(QString const&, int, QChar) const () from /lib64/libQt5Core.so.5 -
@johnyang said in Segmentation Fault with Q_Object when used in a static library [Linux]:
So how can Q_Object macro gets affected by other implementation?
It does not, it's by accident
Do you create a global static QObject-based class somewhere? Use a debugger and look at the backtrace of your crash.
-
@Christian-Ehrlicher ah yes. I found a static QString that is initialised by QString::arg. After changing that, gdb is suggesting the segmentation fault somewhere else at std::atomic::load. That means it's moving away from QString issue. However I commented out everything related to std::atomic in my code but I'm still at the same spot.
It sounds like the problem is no longer related to Qt stuff, but thanks for the help.
-
@johnyang And what is the backtrace of your current thread? I would guess it comes from a global static QObject.
-
@Christian-Ehrlicher it only says seg fault at QString::arg(QString const&, int, QChar) const () from /lib64/libQt5Core.so.5 nothing else. So this is happening before main. However I'm not sure why I can't initialize a global static QString like:
const QString somestring = QString("%1 %2").arg(QString("something")).arg(QString("something"));This code works in Windows but not in linux?
-
@johnyang said in Segmentation Fault with Q_Object when used in a static library [Linux]:
const QString somestring = QString("%1 %2").arg(QString("something")).arg(QString("something"));
I believe this particular one should be OK as it only uses
QString
and that is not aQObject
. However you could temporarily tryconst QString somestring = ""
and see if the error goes away. Do you have otherQString
static variables, or other types, or something which calls something more involved thanQString
? -
@johnyang said in Segmentation Fault with Q_Object when used in a static library [Linux]:
This code works in Windows but not in linux?
By accident. I would guess it's not
"something"
but another QString -
@Christian-Ehrlicher Yes. I think it's to do with that something. It is also a global static QString somewhere. Maybe it's the order of definition.
-
@johnyang said in Segmentation Fault with Q_Object when used in a static library [Linux]:
Maybe it's the order of definition.
Not maybe, it is. The order of the initialization is undefined. Don't do it, you won't need it. Also you should take a look at QStringLiteral - there no initialization is needed