2012-06-21 20 views
2

最近我已經在我的計算機上安裝了Qt庫,作爲一個完整的新手,我查閱了Qt 4.7入門指南在線。樣本提供的Qt堆損壞代碼

就在第一頁上所提供的下面的代碼:

1  #include <QtGui> 
    2 
    3  int main(int argv, char **args) 
    4  { 
    5   QApplication app(argv, args); 
    6 
    7   QTextEdit textEdit; 
    8   QPushButton quitButton("Quit"); 
    9 
10   QObject::connect(&quitButton, SIGNAL(clicked()), qApp, SLOT(quit())); 
11 
12   QVBoxLayout layout; 
13   layout.addWidget(&textEdit); 
14   layout.addWidget(&quitButton); 
15 
16   QWidget window; 
17   window.setLayout(&layout); 
18 
19   window.show(); 
20 
21   return app.exec(); 
22  } 

簡單的東西,我會想。在Visual Studio Express 2010中編寫此代碼,構建和運行時,大多數情況都可以工作。但是,當我嘗試通過「退出」按鈕或顯示窗口右上角的紅色x(啓動「返回app.exec()」)關閉窗口時,我收到以下內容:

一個對話框說:

ParticleTracker.exe中0x77bc15de未處理的異常:0xC0000005:訪問衝突讀取位置0xdf94b4b4。

和控制檯輸出的說法,

Critical error detected c0000374 
Windows has triggered a breakpoint in ParticleTracker.exe. 

This may be due to a corruption of the heap, which indicates a bug in ParticleTracker.exe or any of the DLLs it has loaded. 

This may also be due to the user pressing F12 while ParticleTracker.exe has focus. 

已經進入調試模式,我通過調用堆棧,同時繼續接受反覆堆損壞的錯誤。

First-chance exception at 0x77c6e6c3 in ParticleTracker.exe: 0xC0000374: A heap has been corrupted. 
Unhandled exception at 0x77bc15de in ParticleTracker.exe: 0xC0000374: A heap has been corrupted. 

所有後續異常的發生在可執行0x77bc15de使用存儲器地址0xC0000374的損壞堆。老實說,我並不確定我怎麼能解決這個問題;我對C++並不熟悉,但代碼似乎沒有任何問題。

在調用堆棧,這個過程目前停留在: ParticleTracker.exe主(INT argv的,字符**參數)線20個+ 0×27字節 如果我進入拆​​卸過程是停留在:

return app.exec(); 
00FE3831 mov   esi,esp 
00FE3833 call  dword ptr [__imp_QApplication::exec (0FE93D0h)] 
00FE3839 cmp   esi,esp 
00FE383B call  @ILT+320(__RTC_CheckEsp) (0FE1145h) 
00FE3840 mov   dword ptr [ebp-150h],eax 
00FE3846 mov   byte ptr [ebp-4],5 
00FE384A mov   esi,esp 
00FE384C lea   ecx,[ebp-84h] 
00FE3852 call  dword ptr [__imp_QWidget::~QWidget (0FE9404h)] 
00FE3858 cmp   esi,esp 

任何提示?非常感激。 :)

+0

如果我的回答幫助,請查馬克後分數下讓人們知道,問題就解決了。 – Blood

回答

1

試試這個

#include <QtGui> 

int main(int argv, char **args) 
{ 
     QApplication app(argv, args); 

     QTextEdit *textEdit = new QTextEdit(); 
     QPushButton *quitButton = new QPushButton("Quit"); 

     QObject::connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit())); 

     QVBoxLayout *layout = new QVBoxLayout(); 
     layout->addWidget(textEdit); 
     layout->addWidget(quitButton); 

     QWidget *window = new QWidget(); 
     window->setLayout(layout); 

     window->show(); 

     return app.exec(); 
    } 
0

它可能是關於所有權。當一個小部件被銷燬時,它也會照顧它的孩子,在你的情況下,佈局和子部件。 QWidget析構函數試圖破壞對象,但它們被分配在堆棧上,而不是動態地分配。

嘗試爲QLayouts和小部件使用動態分配。