當我使用繼承(class NapanaApplication : public QGuiApplication
)時,我的非常簡單的例子發生了非常奇怪的行爲,但是在直接使用QGuiApplication
時沒有崩潰並正常工作。C++ - 繼承時的分段錯誤
該代碼完全如下所示,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) {}
調試器輸出:在QString::fromLocal8Bit
中調用strlen
時崩潰。的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
崩潰什麼錯誤? – Surt
@Surt:分段錯誤(Signal SIGSEGV) – Krab