2015-10-22 62 views
1

當我使用繼承(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  
+0

崩潰什麼錯誤? – Surt

+0

@Surt:分段錯誤(Signal SIGSEGV) – Krab

回答

7

Note the signature of QGuiApplication

QGuiApplication(int & argc, char ** argv) 

你的函數有int argc代替。我認爲當整數值被父類構造函數視爲引用時,會導致段錯誤。

+1

Oooo。這將做到這一點。錯誤的數字會導致錯誤的參數數量,導致未終結的char數組上的'strlen'。 – user4581301

+0

@ user4581301有趣的是,只有當你通過'app.exec()'啓動事件循環時纔會發生這種情況?我想在這之前Qt並沒有檢查一些參數。 –

+2

我看着源代碼,這是因爲Qt的存儲的argc作爲參考太,它們在構造函數中初始化它QtConstructor(INT&argcc):的argc(argcc)。如果他們將其存儲爲正常的int,則不會發生任何事情。 – Krab