[Solved] Re-adding the same QPushButton in the same cell of a QTableWidget produces a Segmentation Fault
-
Hello,
I have recently started to code using Qt and this is why I thought that I should first post here. I'm not sure, but I might have found a bug and before reporting it I wanted to make sure I wasn't doing something wrong in my code.
Basically, the title says it all: if I re-add the same push button in the same cell of a QTableWidget, the program eventually crashes with a seg fault. I have made a small program to reproduce this behaviour, the table is created with Qt Creator and in my main program I have:
@WindowContainer::WindowContainer() : QMainWindow()
{
this->setupUi(this);pbChange = new QPushButton("Change It", this);
pbDummy = new QPushButton("Dummy", this);// Connect only the button that will change the other button
QObject::connect(pbChange, SIGNAL(clicked()), this, SLOT(handleClicked()));// Add the buttons in the table
tableWidget->setCellWidget(0, 1, pbChange);
tableWidget->setCellWidget(1, 1, pbDummy);
}void WindowContainer::handleClicked()
{
qDebug()<<"clicked!";
//pbDummy = new QPushButton("Dummy", this); // Uncomment this and it works!
tableWidget->setCellWidget(1, 1, pbDummy);
}@Basically, when pbChange is clicked, pbDummy is re-added in the table. When I run the program, everything looks ok. When I first click pbChange, the dummy button disappears from the table. When I click it a second time, the program seg faults.
Weirdly enough, if the pbDummy button is created each time before adding it in the cell (i.e. by uncommenting line 19), nothing unusual happens, everything works as it should (the button doesn't disappear, the program doesn't crash).
Can anyone tell me if anything looks suspicious in this code, have I done something wrong? If you run this, does the same thing happen on your machine? If you need more information I will be happy to provide it.
I use Qt 4.8.4, g++ 4.6.4, under the Operating System Linux Mint 15 Olivia (32-bit).
Cheers,
Silvia -
welcome to devnet
With setCellWidget you handover the "ownership of the widget":http://qt-project.org/doc/qt-5.0/qtwidgets/qtablewidget.html#setCellWidget to tableWidget. The second time you try to handover something you do not have anymore, the ownership of that widget. That does also explain why it does work when you are uncommenting line 19. You are creating a new button. If the handling of the previous would not be with the tableWidget you would create there a memory leak, but in this case it is fine.
-
Sorry, I have used a reference to Qt5 documentation, but for Qt 4 it is "the same information.":http://qt-project.org/doc/qt-4.8/qtablewidget.html#setCellWidget
So, it is not a bug of Qt libs.
-
Thank you for your answer, koahnig!
I think I understand now. It is still not clear to me why the first time the button disappears and only the second time it gives a seg fault (why doesn't it just seg fault directly), but I get the big picture.
-
Hmmm... Just another quick question: I understand that this behavior is by design for the setCellWidget, but shouldn't it also handle somehow this case? I mean, in some other way than just seg fault...
-
When you set another QPushButton to the CellWidget, the CellWidget will first delete the old QPushButton it has. But since you are setting the same QPushButton again, pbDummy is now pointing to an invalid block of memory. That is why you get a segmentation fault.
The same would happen if you delete pbDummy yourself. Not much CellWidget can do to prevent this behaviour. -
Thank you, t3685! This is so much clearer now :)
-
[quote author="t3685" date="1378391383"]When you set another QPushButton to the CellWidget, the CellWidget will first delete the old QPushButton it has. But since you are setting the same QPushButton again, pbDummy is now pointing to an invalid block of memory. That is why you get a segmentation fault.
The same would happen if you delete pbDummy yourself. Not much CellWidget can do to prevent this behaviour.[/quote]Nothing to add to this answer :-)