PDA

View Full Version : Resetting rows in Table causes memory leak leading to crash



deepakn
21st July 2011, 10:19
Hi,

I have a QTableWidget in my app which gets updated every second. PFB the code which performs the updation:
QStringList detstatsList = lStr2.split("|");//The list contains data to be loaded into the table
int row = 0;
ui.trafficStatsTable->setRowCount(0);//This line seems to be causing the problem :S
for (int m = 0; m < detstatsList.size(); ++m) {
QTableWidgetItem *resourceItem = new QTableWidgetItem(detstatsList[m]);
if(m%4 == 0)
{
row = ui.trafficStatsTable->rowCount();
ui.trafficStatsTable->insertRow(row);
ui.trafficStatsTable->setItem(row, 0, resourceItem);
}
if(m%4 == 1)
ui.trafficStatsTable->setItem(row, 1, resourceItem);
if(m%4 == 2)
ui.trafficStatsTable->setItem(row, 2, resourceItem);
if(m%4 == 3)
ui.trafficStatsTable->setItem(row, 3, resourceItem);
//delete resourceItem;
}
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):

(gdb) bt
#0 0x00a08402 in __kernel_vsyscall ()
#1 0x41a9bfa0 in raise () from /lib/libc.so.6
#2 0x41a9d8b1 in abort () from /lib/libc.so.6
#3 0x41ad2ebb in __libc_message () from /lib/libc.so.6
#4 0x41adaf41 in _int_free () from /lib/libc.so.6
#5 0x41ade580 in free () from /lib/libc.so.6
#6 0x00abd0a1 in operator delete () from /usr/lib/libstdc++.so.6
#7 0x0076636b in QTableWidgetItem::~QTableWidgetItem$delete () from /usr/lib/libQtGui.so.4
#8 0x007668fd in QTableModel::removeRows () from /usr/lib/libQtGui.so.4
#9 0x00762e1e in QTableModel::setRowCount () from /usr/lib/libQtGui.so.4
#10 0x00769ee4 in QTableWidget::setRowCount () from /usr/lib/libQtGui.so.4
#11 0x0806e721 in Dialog::carryOutUpdate ()

high_flyer
21st July 2011, 13:59
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.

deepakn
22nd July 2011, 07:36
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 :p )

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.

Santosh Reddy
22nd July 2011, 08:30
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)

deepakn
22nd July 2011, 10:42
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. :D