Saturday, May 17, 2014

Solved: Qt Segmentation fault on MainWindow show() on line: inline void show() { setVisible(true); }

Writing this down so I'll remember in the future.

The application worked in Qt Creator in Windows, but in Linux we received Segmentation Fault when trying to run the application in the debugger.

Looking at the stacktrace only showed that it crashed in SetVisible on the main window.

0 __strlen_sse2 strlen.S 99 0x7e9520
1 XSetCommand /usr/lib/libX11.so.6 0xdbbb9f
2 XSetWMProperties /usr/lib/libX11.so.6 0xdc0601
3 QWidgetPrivate::create_sys(unsigned long, bool, bool) /opt/qt-4.8.4/lib/libQtGui.so.4 0x110db5e
4 QWidget::create(unsigned long, bool, bool) /opt/qt-4.8.4/lib/libQtGui.so.4 0x10bc73c
5 QWidget::setVisible(bool) /opt/qt-4.8.4/lib/libQtGui.so.4 0x10c20e5
6 QWidget::show qwidget.h 494 0x804fd0c
7 mgccis::shell::MainApplication::InitializeInterface mainapplication.cpp 29 0x804facc
8 main main.cpp 25 0x804efb0



It stopped on the line below, it made no sense all objects where initialized:

#ifndef Q_WS_WINCE
    inline void show() { setVisible(true); }
#else
    void show();
#endif



After some googling of the subject I found this forum post that described a problem that can cause this error message, it's the signature on the MainApplication that can causes the problem:
http://www.qtcentre.org/archive/index.php/t-45135.html?s=ea17a4ab05c00533d49f938c1da47077


But our solutionwas the opposite from the resolution in the forum post. We have our own builder class that gets the arguments from the main method and that passes the argument to the MainApplication, so in our case the solution was to send the argc parameter as value instead of a reference:

So the Change was from:
explicit MainApplication(int &argc, char* argv[]);


To this:
explicit MainApplication(int argc, char** argv);


After the change the application can be debugged in Windows and Linux and the error message made sense, we sent a reference to a temporary instance of the argc to the MainWindow that used when window is shown.