PDA

View Full Version : Access violation using QTableWidget



mchome
3rd September 2012, 23:39
I want to use the QTableWidget component in a program of mine.
In trying to do so I came across repeated crash of the program.
I could build a tiny program still showing this bad behaviour:


#include <QtGui/QApplication>
#include <QPushButton>
#include <QTableWidget>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
window.resize(300,200);

QTableWidgetItem items[5];
QTableWidget varTable(&window);
QString str;
varTable.setRowCount(5);
varTable.setColumnCount(1);

for(int i=0; i<5; i++){
str.setNum(i);
items[i].setText(str);
varTable.setItem(i,0,items+i);
}
varTable.clearContents();
window.show();
return app.exec();
}

In this program the crash occurs when executing the varTable.clearContents() row (row 22).
I cannot envisage what is wrong.
Removing that row (that is not what I want) does not eliminate program crashing, that in this case occurs at program end, when automatic freeing of memory is made.

This is part of the error message I get on my Mac:

Process: tableTest [22320]
Path: /Users/USER/Documents/*/tableTest.app/Contents/MacOS/tableTest
Identifier: com.yourcompany.tableTest
Version: ??? (???)
Code Type: X86-64 (Native)
Parent Process: Qt Creator [20992]

Date/Time: 2012-09-03 23:27:18.638 +0200
OS Version: Mac OS X 10.7.4 (11E53)
Report Version: 9

Interval Since Last Report: 160925 sec
Crashes Since Last Report: 44
Per-App Interval Since Last Report: 24 sec
Per-App Crashes Since Last Report: 6
Anonymous UUID: 43BD944D-587A-4F37-B17A-CBFE26F01C4F

Crashed Thread: 0 Dispatch queue: com.apple.main-thread

Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000

Application Specific Information:
objc[22320]: garbage collection is OFF
*** error for object 0x7fff5fbff9c8: pointer being freed was not allocated


Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libsystem_kernel.dylib 0x00007fff8e410ce2 __pthread_kill + 10
1 libsystem_c.dylib 0x00007fff8c7987d2 pthread_kill + 95
2 libsystem_c.dylib 0x00007fff8c789a7a abort + 143
3 libsystem_c.dylib 0x00007fff8c7e884c free + 389
4 QtGui 0x000000010060ab2b QTableModel::clearContents() + 123
5 com.yourcompany.tableTest 0x0000000100002fdb main + 779 (main.cpp:22)
6 com.yourcompany.tableTest 0x000000010000289a _start + 248
7 com.yourcompany.tableTest 0x00000001000027a1 start + 33


the third before last row confirms that the issue is on clearContents() function; the previous shows that the issue occurs because the program cannot free the memory correctly.

Someone can help?

Thank in advance.

norobro
4th September 2012, 04:05
QTableWidget::clearContents() calls delete (link (http://qt.gitorious.org/qt/qt/blobs/4.8/src/gui/itemviews/qtablewidget.cpp#line756)) on each QTableWidgetItem. In this case delete is being called on objects not allocated with new (stack allocated objects) which will yield unpredictable results. You need to allocate your QTableWidgetItem(s) on the heap:
. . .
//QTableWidgetItem items[5];
QTableWidget varTable(&window);
QString str;
varTable.setRowCount(5);
varTable.setColumnCount(1);

for(int i=0; i<5; i++){
str.setNum(i);
QTableWidgetItem *item = new QTableWidgetItem(str);
varTable.setItem(i,0,item);
}
varTable.clearContents();
. . .

mchome
4th September 2012, 08:39
You explained everything.
From the error message I envisaged freeing problems, but did not think that, since I allocated items statically, any call to "free" (even internally from the QTableWidget object) would cause unpredictable results.

Thanks a lot.