Results 1 to 5 of 5

Thread: Resetting rows in Table causes memory leak leading to crash

  1. #1
    Join Date
    Jun 2007
    Location
    India
    Posts
    33
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation Resetting rows in Table causes memory leak leading to crash

    Hi,

    I have a QTableWidget in my app which gets updated every second. PFB the code which performs the updation:
    Qt Code:
    1. QStringList detstatsList = lStr2.split("|");//The list contains data to be loaded into the table
    2. int row = 0;
    3. ui.trafficStatsTable->setRowCount(0);//This line seems to be causing the problem :S
    4. for (int m = 0; m < detstatsList.size(); ++m) {
    5. QTableWidgetItem *resourceItem = new QTableWidgetItem(detstatsList[m]);
    6. if(m%4 == 0)
    7. {
    8. row = ui.trafficStatsTable->rowCount();
    9. ui.trafficStatsTable->insertRow(row);
    10. ui.trafficStatsTable->setItem(row, 0, resourceItem);
    11. }
    12. if(m%4 == 1)
    13. ui.trafficStatsTable->setItem(row, 1, resourceItem);
    14. if(m%4 == 2)
    15. ui.trafficStatsTable->setItem(row, 2, resourceItem);
    16. if(m%4 == 3)
    17. ui.trafficStatsTable->setItem(row, 3, resourceItem);
    18. //delete resourceItem;
    19. }
    To copy to clipboard, switch view to plain text mode 
    The program crashed out after few updates. The backtrace from gdb is given below. I am not able to figure out why removing rows is leading to a memory trouble. Could someone please help?

    Backtrace(starting from the function call containing above code):
    Qt Code:
    1. (gdb) bt
    2. #0 0x00a08402 in __kernel_vsyscall ()
    3. #1 0x41a9bfa0 in raise () from /lib/libc.so.6
    4. #2 0x41a9d8b1 in abort () from /lib/libc.so.6
    5. #3 0x41ad2ebb in __libc_message () from /lib/libc.so.6
    6. #4 0x41adaf41 in _int_free () from /lib/libc.so.6
    7. #5 0x41ade580 in free () from /lib/libc.so.6
    8. #6 0x00abd0a1 in operator delete () from /usr/lib/libstdc++.so.6
    9. #7 0x0076636b in QTableWidgetItem::~QTableWidgetItem$delete () from /usr/lib/libQtGui.so.4
    10. #8 0x007668fd in QTableModel::removeRows () from /usr/lib/libQtGui.so.4
    11. #9 0x00762e1e in QTableModel::setRowCount () from /usr/lib/libQtGui.so.4
    12. #10 0x00769ee4 in QTableWidget::setRowCount () from /usr/lib/libQtGui.so.4
    13. #11 0x0806e721 in Dialog::carryOutUpdate ()
    To copy to clipboard, switch view to plain text mode 
    Design is easy. All you do is stare at the screen until drops of blood form on your forehead.
    -Marty Neumeier

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Resetting rows in Table causes memory leak leading to crash

    Are you accessing the items somewhere else in your code?
    And maybe deleting them?

    By the way, you are using '++i' in your for loop, I guess in order to win some performance.
    But you are losing performance when you code goes each time through ALL if statements (which are expecive!).
    Either put an if-else construct or a case-switch.
    You also better access the list elements through at() and not through '[]' operator.
    And if we are at it, you should make sure that if none of your 'if's was executed, that you delete the item, since it will not be assigned to the table, and thus be a mem leak.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    deepakn (22nd July 2011)

  4. #3
    Join Date
    Jun 2007
    Location
    India
    Posts
    33
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resetting rows in Table causes memory leak leading to crash

    Thanks high_flyer for the quick response
    I have modified my code according to the suggestions. It seems that i had been using [] at a lot of places (had become a habit )

    The solution, strange though, was not with QTableWidget or not even Qt code but was due to certain pointer/nan issue in 'C' backend (The exact problem we are yet to uncover ). Extensive testing led us to different backtraces every time same error occurred. We were confused. Sometimes it was with QLayout, sometimes QFont, and so on... Then we discarded gdb and went down to code and the test case. In the testing scenario, we were using set of files which would be analysed by the C backend to do some calculations. Among them, one file was corrupt (did not have enough data) which was leading to certain 'not a number' (and occasionally -inf conditions) and thus to the crash. I don't know why gdb had been spitting strange unrelated stack info.

    I am curious about this particular issue and would to like ask if you guys know any good OSS project which has Qt front end and C backend. Would help us much in getting things straight.
    Our project has C backend(as an indepenedent library) & QT front end and data is passed through a shared structure which is included as a part of backend libraries. I have had a lot of issues initially to get the stuff even to compile. Now I am doubting if this setup may cause further trouble. This started off as a tiny project and now it is bound to scale up and I don't want it to bust in the long run.

    Thanks again for the support.
    Design is easy. All you do is stare at the screen until drops of blood form on your forehead.
    -Marty Neumeier

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Resetting rows in Table causes memory leak leading to crash

    There are couple of things in your code which may cause you problems, (may not be the problem which you see now)

    1. Disable sorting (if enabled) on the QTableWidget when inserting rows, and enable back when insertion and item setting is done.
    2. There is no check on m (detstatsList.size()) for it to be multiple of 4 (a row with few empty columns)

  6. The following user says thank you to Santosh Reddy for this useful post:

    deepakn (22nd July 2011)

  7. #5
    Join Date
    Jun 2007
    Location
    India
    Posts
    33
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resetting rows in Table causes memory leak leading to crash

    Hi Santhosh,

    Thanks for the response. We found that the particular segment of code is not the one causing segfault. But I was able to find out many issues in the code even though they were not direct troublemakers, thanks to you and high_flyer. Most parts of this were written years ago when i was starting out with Qt and I guess I will have to do a detailed review now.
    Design is easy. All you do is stare at the screen until drops of blood form on your forehead.
    -Marty Neumeier

Similar Threads

  1. Qt example with memory leak
    By Squall in forum Qt Programming
    Replies: 5
    Last Post: 21st February 2011, 11:07
  2. Memory leak
    By yxtx1984 in forum Qt Programming
    Replies: 4
    Last Post: 26th February 2010, 12:13
  3. Qt dll + memory leak
    By Fastman in forum Qt Programming
    Replies: 3
    Last Post: 2nd August 2009, 14:28
  4. Memory Leak in Qt
    By Krish_ng in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2007, 09:02
  5. Memory leak
    By zlatko in forum Qt Programming
    Replies: 8
    Last Post: 28th March 2006, 20:02

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.