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:
Qt Code:
  1. #include <QtGui/QApplication>
  2. #include <QPushButton>
  3. #include <QTableWidget>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7. QApplication app(argc, argv);
  8. QWidget window;
  9. window.resize(300,200);
  10.  
  11. QTableWidgetItem items[5];
  12. QTableWidget varTable(&window);
  13. QString str;
  14. varTable.setRowCount(5);
  15. varTable.setColumnCount(1);
  16.  
  17. for(int i=0; i<5; i++){
  18. str.setNum(i);
  19. items[i].setText(str);
  20. varTable.setItem(i,0,items+i);
  21. }
  22. varTable.clearContents();
  23. window.show();
  24. return app.exec();
  25. }
To copy to clipboard, switch view to plain text mode 
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.