Reputation: 284
I'm pretty new to QT development, and I'm trying to create my first window with a menu. The problem is that I get a segmentation fault whenever I actually click on the File menu when trying to debug it.
This is QT 5.10, running on Fedora Linux 64-bit
My header file has:
private:
QApplication app;
Ui::MainWindow ui;
QMainWindow mainWindow;
public:
explicit ProgName(int argc, char *argv[], QObject *parent = nullptr);
int run();
...and the rest is just standard QT boilerplate omitted for brevity. My main source file:
#include "progname.h"
int main(int argc, char *argv[])
{
ProgName pn(argc, argv, nullptr);
return pn.run();
}
ProgName::ProgName(int argc, char *argv[], QObject *parent) :
QObject(parent),
app(argc, argv)
{
ui.setupUi(&mainWindow);
}
int ProgName::run()
{
mainWindow.show();
return app.exec();
}
And the really long one, the UI file, made with QT creator:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QTreeView" name="mainTreeView"/>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="mainMenuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>28</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
</property>
</widget>
<addaction name="menuFile"/>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
Any ideas?
Edit: Here's the stack trace:
1 __strlen_avx2 0x7ffff5832c37
2 QCoreApplication::arguments() 0x7ffff6a7a63b
3 argv0BaseName() 0x7fffe88d0101
4 QXcbIntegration::wmClass() const 0x7fffe88d05fd
5 QXcbWindow::create() 0x7fffe88e596b
6 QXcbIntegration::createPlatformWindow(QWindow *) const 0x7fffe88d153e
7 QWindowPrivate::create(bool, unsigned long long) 0x7ffff6fd32fe
8 QWidgetPrivate::create_sys(unsigned long long, bool, bool) 0x7ffff7714ced
9 QWidget::create(unsigned long long, bool, bool) 0x7ffff77153ad
10 QMenuPrivate::adjustMenuScreen(QPoint const&) 0x7ffff785bf68
11 QMenu::popup(QPoint const&, QAction *) 0x7ffff785f801
12 QMenuBarPrivate::popupAction(QAction *, bool) 0x7ffff786c402
13 QMenuBarPrivate::setCurrentAction(QAction *, bool, bool) 0x7ffff786e508
14 QMenuBar::mousePressEvent(QMouseEvent *) 0x7ffff786ee72
15 QWidget::event(QEvent *) 0x7ffff7722baf
16 QMenuBar::event(QEvent *) 0x7ffff787014b
17 QApplicationPrivate::notify_helper(QObject *, QEvent *) 0x7ffff76e392c
18 QApplication::notify(QObject *, QEvent *) 0x7ffff76eb6cf
19 QCoreApplication::notifyInternal2(QObject *, QEvent *) 0x7ffff6a76be7
20 QApplicationPrivate::sendMouseEvent(QWidget *, QMouseEvent *, QWidget *, QWidget *, QWidget * *, QPointer<QWidget>&, bool) 0x7ffff76ea6a2
21 QWidgetWindow::handleMouseEvent(QMouseEvent *) 0x7ffff773d47b
22 QWidgetWindow::event(QEvent *) 0x7ffff773fb1f
23 QApplicationPrivate::notify_helper(QObject *, QEvent *) 0x7ffff76e392c
24 QApplication::notify(QObject *, QEvent *) 0x7ffff76eb174
25 QCoreApplication::notifyInternal2(QObject *, QEvent *) 0x7ffff6a76be7
26 QGuiApplicationPrivate::processMouseEvent(QWindowSystemInterfacePrivate::MouseEvent *) 0x7ffff6fc98a3
27 QGuiApplicationPrivate::processWindowSystemEvent(QWindowSystemInterfacePrivate::WindowSystemEvent *) 0x7ffff6fcb495
28 QWindowSystemInterface::sendWindowSystemEvents(QFlags<QEventLoop::ProcessEventsFlag>) 0x7ffff6fa479b
29 userEventSourceDispatch(_GSource *, int ( *)(void *), void *) 0x7fffe892cb60
30 g_main_context_dispatch 0x7ffff2195b77
31 g_main_context_iterate.isra 0x7ffff2195f20
32 g_main_context_iteration 0x7ffff2195fac
33 QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) 0x7ffff6ac7c2f
34 QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) 0x7ffff6a7596a
35 QCoreApplication::exec() 0x7ffff6a7e094
36 ProgName::run progname.cpp 21 0x401aaf
37 main progname.cpp 7 0x401961
Upvotes: 1
Views: 1072
Reputation: 735
Please have a look at the constructors of QCoreApplication and QApplication.
The constructors expect the argc
argument to be a reference to an integer.
In your case, you pass the argc
argument by value to the constructor of your ProgName
class. Inside, you pass a reference to this (local) value down to the constructor of QApplication
. The issue is that the reference will be invalid/dangling, as soon as the constructor call is finished. In particular, when calling exec()
later in the run
method, the application object will try to access the reference which fails and produces the observed crash.
Long story short: as a workaround, just pass through the argc
argument as a reference:
ProgName::ProgName(int &argc, char *argv[], QObject *parent) :
QObject(parent),
app(argc, argv)
{
ui.setupUi(&mainWindow);
}
Upvotes: 3