Results 1 to 3 of 3

Thread: Access violation using QTableWidget

  1. #1
    Join Date
    May 2012
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Access violation using QTableWidget

    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.

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Access violation using QTableWidget

    QTableWidget::clearContents() calls delete (link) 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:
    Qt Code:
    1. . . .
    2. //QTableWidgetItem items[5];
    3. QTableWidget varTable(&window);
    4. QString str;
    5. varTable.setRowCount(5);
    6. varTable.setColumnCount(1);
    7.  
    8. for(int i=0; i<5; i++){
    9. str.setNum(i);
    10. varTable.setItem(i,0,item);
    11. }
    12. varTable.clearContents();
    13. . . .
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to norobro for this useful post:

    mchome (4th September 2012)

  4. #3
    Join Date
    May 2012
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Access violation using QTableWidget

    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.

Similar Threads

  1. Access violation when closing application
    By doggrant in forum Qt Programming
    Replies: 8
    Last Post: 15th March 2012, 15:07
  2. Access violation -- qobject.cpp
    By willief in forum Newbie
    Replies: 9
    Last Post: 14th February 2011, 22:55
  3. Access Violation - Crashing Application
    By LIRiot in forum Qt Programming
    Replies: 9
    Last Post: 5th December 2010, 23:10
  4. Access Violation with VS2008
    By Takatschio in forum Qt Programming
    Replies: 3
    Last Post: 19th August 2010, 09:16
  5. QModelIndexList access reading violation
    By Daxos in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2010, 08:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.