我的示例很简单,当我使用继承(class NapanaApplication : public QGuiApplication
)时崩溃,但是当直接使用QGuiApplication
时,它不会崩溃并且可以正常工作。
代码完全如下所示,NapanaApplication
不执行任何操作,仅继承自QGuiApplication
。
怎么了?
解决:我找到了一个原因。这是因为QGuiApplication
将argc
作为引用,但NapanaApplication
构造函数却没有。它不会产生任何错误,但是会导致QGuiApplication
使用某个临时argc
变量而不是main
中的变量进行操作。
main.cpp
#include "napanaapplication.h"
#include "napanawindow.h"
int main(int argc, char* argv[]) {
/*NapanaApplication app(argc, argv);*/ // segmentation fault
QGuiApplication app(argc, argv); // no error
NapanaWindow win;
win.resize(800, 600);
win.show();
return app.exec();
}
napanaapplication.h
#ifndef NAPANAAPPLICATION_H
#define NAPANAAPPLICATION_H
#include <QGuiApplication>
class NapanaApplication : public QGuiApplication
{
Q_OBJECT
public:
explicit NapanaApplication(int argc, char* argv[]);
signals:
public slots:
};
#endif // NAPANAAPPLICATION_H
napanapplication.c
#include "napanaapplication.h"
NapanaApplication::NapanaApplication(int argc, char* argv[]) : QGuiApplication(argc, argv) {}
调试器的输出:在
strlen
中调用QString::fromLocal8Bit
时崩溃。 调用char*
的strlen
指针具有值0x21
,我不明白为什么,它可能应该是argv
中的值。0 strlen /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.19.so 106 0x7ffff65c0aea
1 QString::fromLocal8Bit qstring.h 534 0x7ffff7142c11
2 QCoreApplication::arguments qcoreapplication.cpp 2254 0x7ffff738503d
3 sm_performSaveYourself qxcbsessionmanager.cpp 188 0x7ffff05f5e3a
4 sm_saveYourselfCallback qxcbsessionmanager.cpp 171 0x7ffff05f5cc9
5 _SmcProcessMessage 0x7ffff014ad37
6 IceProcessMessages 0x7fffeff3b8c7
7 QSmSocketReceiver::socketActivated qxcbsessionmanager.cpp 322 0x7ffff05f67e0
8 QSmSocketReceiver::qt_static_metacall qxcbsessionmanager.moc 68 0x7ffff05f6f88
9 QMetaObject::activate qobject.cpp 3718 0x7ffff73c097e
10 QMetaObject::activate qobject.cpp 3583 0x7ffff73c016c
11 QSocketNotifier::activated moc_qsocketnotifier.cpp 134 0x7ffff7458376
12 QSocketNotifier::event qsocketnotifier.cpp 296 0x7ffff73cb4db
13 QCoreApplicationPrivate::notify_helper qcoreapplication.cpp 1093 0x7ffff7382872
14 QCoreApplication::notify qcoreapplication.cpp 1038 0x7ffff7382554
15 QGuiApplication::notify qguiapplication.cpp 1537 0x7ffff77f3076
16 QCoreApplication::notifyInternal qcoreapplication.cpp 965 0x7ffff738245e
17 QCoreApplication::sendEvent qcoreapplication.h 224 0x7ffff7386087
18 socketNotifierSourceDispatch qeventdispatcher_glib.cpp 101 0x7ffff73f6afe
19 g_main_context_dispatch 0x7ffff4f8dbd4
20 ?? 0x7ffff4f8de18
21 g_main_context_iteration 0x7ffff4f8debc
22 QEventDispatcherGlib::processEvents qeventdispatcher_glib.cpp 418 0x7ffff73f7799
23 QPAEventDispatcherGlib::processEvents qeventdispatcher_glib.cpp 115 0x7ffff0648906
24 QEventLoop::processEvents qeventloop.cpp 128 0x7ffff737f0b2
25 QEventLoop::exec qeventloop.cpp 204 0x7ffff737f373
26 QCoreApplication::exec qcoreapplication.cpp 1229 0x7ffff7382b36
27 QGuiApplication::exec qguiapplication.cpp 1528 0x7ffff77f3026
28 main main.cpp 13 0x40275a
最佳答案
Note the signature of QGuiApplication:
QGuiApplication(int & argc, char ** argv)
您的函数改用
int argc
。我认为当整数值被父类构造函数视为引用时,您正在引起段错误。关于c++ - C++-继承时的段错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33271398/