PDA

View Full Version : SIGSEGV Error: Very Strange



grayfox
9th October 2011, 17:31
MainWindow.h


#include <QMainWindow>

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
};


MainWindow.cpp


#include "MainWindow.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
#ifdef QT_DEBUG
setWindowTitle("DEBUG");
#else
setWindowTitle("RELEASE");
#endif

}


Application.h


#include <QApplication>

class MainWindow;

class Application : public QApplication
{
public:
Application(int argc, char** argv);
~Application();
MainWindow* mainWindow() const;
private:
MainWindow* _mainWindow;
};

Application.cpp


#include "Application.h"
#include "MainWindow.h"

Application::Application(int argc, char** argv) : QApplication(argc, argv)
{
_mainWindow = new MainWindow;
}

Application::~Application(){
delete _mainWindow;
_mainWindow = 0;
}

MainWindow * Application::mainWindow() const
{
return _mainWindow;
}


main.cpp


#include "Application.h"
#include "MainWindow.h"

int main(int argc, char** argv)
{
Application app(argc, argv);
app.mainWindow()->show();
return app.exec();
}




The code above run perfectly fine under release build, but will cause SIGSEGV Error everytime under debug build.
My system is Ubuntu 11.04 64-bits, Qt 4.8/4.7.4

Santosh Reddy
9th October 2011, 17:38
When does SIGSEGV error occur?

You need to have virtual destructor
virtual ~Application()

grayfox
9th October 2011, 17:55
When does SIGSEGV error occur?

You need to have virtual destructor
virtual ~Application()
Exact same thing happens even after I added virtual.
The error occur in qwidget.h at line 494


inline void show() { setVisible(true); }


basically when I call "app.mainWindow()->show();" in main.

wysota
9th October 2011, 18:18
Does app.mainWindow() return a non-null pointer?

grayfox
10th October 2011, 08:39
Does app.mainWindow() return a non-null pointer?
Yes, it is not null.

wysota
10th October 2011, 08:47
What does the backtrace say?

grayfox
10th October 2011, 11:37
What does the backtrace say?


Thread 1 (Thread 0x7ffff7fca7a0 (LWP 12791)):
#0 0x00007ffff5d9b49f in ?? () from /lib/x86_64-linux-gnu/libc.so.6
No symbol table info available.
#1 0x00007ffff4310382 in XSetCommand () from /usr/lib/x86_64-linux-gnu/libX11.so.6
No symbol table info available.
#2 0x00007ffff4315169 in XSetWMProperties () from /usr/lib/x86_64-linux-gnu/libX11.so.6
No symbol table info available.
#3 0x00007ffff735420a in QWidgetPrivate::create_sys(unsigned long, bool, bool) () from /usr/local/Trolltech/Qt-4.8.0/lib/libQtGui.so.4
No symbol table info available.
#4 0x00007ffff73058e5 in QWidget::create(unsigned long, bool, bool) () from /usr/local/Trolltech/Qt-4.8.0/lib/libQtGui.so.4
No symbol table info available.
#5 0x00007ffff730f3d1 in QWidget::setVisible(bool) () from /usr/local/Trolltech/Qt-4.8.0/lib/libQtGui.so.4
No symbol table info available.
#6 0x0000000000402a5c in QWidget::show (this=0x609ab0) at /usr/local/Trolltech/Qt-4.8.0/include/QtGui/qwidget.h:494
No locals.
#7 0x00000000004029fd in main (argc=1, argv=0x7fffffffe6e8) at Main.cpp:7
app = {<QApplication> = {<No data fields>}, _mainWindow = 0x609ab0}

wysota
10th October 2011, 11:40
Does it work if you use a stable version of Qt?

grayfox
10th October 2011, 14:07
Does it work if you use a stable version of Qt?

Same result with Qt 4.7.4

wysota
10th October 2011, 14:29
It seems odd the application crashes when setting window properties. What window manager are you using? Does the program work if you rewrite it as:


int main(int argc, char **argv) {
QApplication app(argc, argv);
QMainWindow mw;
mw.setWindowTitle("DEBUG");
mw.show();
return app.exec();
}

grayfox
10th October 2011, 15:23
It seems odd the application crashes when setting window properties. What window manager are you using? Does the program work if you rewrite it as:


int main(int argc, char **argv) {
QApplication app(argc, argv);
QMainWindow mw;
mw.setWindowTitle("DEBUG");
mw.show();
return app.exec();
}

This works fine.

Added after 10 minutes:

Ok, now this code will reproduce the issue



#include <QWidget>
#include <QApplication>

class MyApplication : public QApplication
{
public:
MyApplication(int argc, char** argv) : QApplication(argc, argv){}
};

int main(int argc, char** argv)
{
MyApplication app(argc, argv);
QWidget w;

// fail on this line when CONFIG+=debug
// (qmake my_project.pro -r -spec linux-g++-64 CONFIG+=debug)
w.show();
return app.exec();
}



If I change "MyApplication app(argc, argv);" to "QApplication app(argc, argv);", everything will works fine

wysota
10th October 2011, 15:31
And this?


class MainWindow : public QMainWindow {
public:
MainWindow() : QMainWindow() { setWindowTitle("DEBUG"); }
};

int main(int argc, char **argv) {
QApplication app(argc, argv);
MainWindow mw;
mw.show();
return app.exec();
}

If it works then check this:


class MainWindow : public QMainWindow {
public:
MainWindow() : QMainWindow() { setWindowTitle("DEBUG"); }
};

class Application : public QApplication {
public:
Application(int argc, char **argv) : QApplication(argc, argv) {}
};

int main(int argc, char **argv) {
Application app(argc, argv);
MainWindow mw;
mw.show();
return app.exec();
}

and then this one:


class MainWindow : public QMainWindow {
public:
MainWindow() : QMainWindow() { setWindowTitle("DEBUG"); }
};

class Application : public QApplication {
public:
Application(int argc, char **argv) : QApplication(argc, argv) {}
MainWindow *mainWindow() const { return &mw; }
private:
MainWindow mw;
};

int main(int argc, char **argv) {
Application app(argc, argv);
app.mainWindow()->show();
return app.exec();
}

grayfox
10th October 2011, 16:01
First 2 examples work, but the third one can't compile on line

MainWindow *mainWindow() const { return &mw; }
error: invalid conversion from ‘const MainWindow*’ to ‘MainWindow*’
I removed the 'const' and it compiles, this time, it doesn't work, exact same problem, OK in release mode, error in debug mode, same result with Qt4.7.4 and Qt4.8

wysota
10th October 2011, 16:06
Ok, then this should work:

class MainWindow : public QMainWindow {
public:
MainWindow() : QMainWindow() { setWindowTitle("DEBUG"); }
};

class Application : public QApplication {
public:
Application(int argc, char **argv) : QApplication(argc, argv) { mw = 0; }
void init() {
mw = new MainWindow;
}
~Application() { delete mw; }
MainWindow *mainWindow() const { return mw; }
private:
MainWindow *mw;
};

int main(int argc, char **argv) {
Application app(argc, argv);
app.init();
app.mainWindow()->show();
return app.exec();
}

grayfox
10th October 2011, 16:47
The above code works, but the problem still exists.

This code will fail when compiled under debug mode.


#include <QWidget>
#include <QApplication>

class Application : public QApplication
{
public:
Application(int argc, char** argv) : QApplication(argc, argv){}
};

int main(int argc, char** argv)
{
Application a(argc, argv);
QWidget w;
w.show(); // fail
return a.exec();
}



The backtrace is:

Thread 1 (Thread 0x7ffff7fca7a0 (LWP 25415)):
#0 0x00007ffff5d9b49f in ?? () from /lib/x86_64-linux-gnu/libc.so.6
No symbol table info available.
#1 0x00007ffff4310382 in XSetCommand () from /usr/lib/x86_64-linux-gnu/libX11.so.6
No symbol table info available.
#2 0x00007ffff4315169 in XSetWMProperties () from /usr/lib/x86_64-linux-gnu/libX11.so.6
No symbol table info available.
#3 0x00007ffff735420a in QWidgetPrivate::create_sys(unsigned long, bool, bool) () from /usr/local/Trolltech/Qt-4.8.0/lib/libQtGui.so.4
No symbol table info available.
#4 0x00007ffff73058e5 in QWidget::create(unsigned long, bool, bool) () from /usr/local/Trolltech/Qt-4.8.0/lib/libQtGui.so.4
No symbol table info available.
#5 0x00007ffff730f3d1 in QWidget::setVisible(bool) () from /usr/local/Trolltech/Qt-4.8.0/lib/libQtGui.so.4
No symbol table info available.
#6 0x0000000000401226 in QWidget::show (this=0x7fffffffe5b0) at /usr/local/Trolltech/Qt-4.8.0/include/QtGui/qwidget.h:494
No locals.
#7 0x00000000004011ab in main (argc=1, argv=0x7fffffffe6f8) at Main.cpp:14
a = {<QApplication> = {<No data fields>}, <No data fields>}
w = <incomplete type>

wysota
10th October 2011, 17:26
Change the Application constructor to:

Application(int &argc, char **argv) : QApplication (argc, argv) {}

simoncsmith
13th January 2012, 20:01
I just came across this very same issue under Fedora 15(x64) with Qt 4.7.4.

Do you have any explanation as to *why* adding a reference to the constructor worked wysota? (and how you deduced that was the problem).

Simon.

amleto
13th January 2012, 20:44
http://developer.qt.nokia.com/doc/qt-4.8/qapplication.html
make sure the signature matches. otherwise, in this case, qapplication is storing a referece to a temporary

simoncsmith
16th January 2012, 15:50
Thanks Amleto - I have to admit that I did not see that QApplication takes a *reference* to an integer in its prototype, most likely because I'm so used to seeing it without it!