Unsolved QTableView deleting causes a fault.
-
I've created an instance of QTableView:
mptvTrecs = new QTableView(this); mptvTrecs->setSelectedBehaviour(QAbstractItemView::SelectRows); mptvTrecs->setStyleSheet(DataSets::mscszTableViewStyle); mpsiTmodel = new QStandardItemModel(this); mptvTrecs->setModel(mpsiTModel);
The actual source that adds rows to the table hasn't been called until a Tab is selected in the dialog. If I close the dialog, the clean up looks like this:
if ( mptvTrecs != nullptr ) { delete mptvTrecs; } if ( mpsiTmodel != nullptr ) { delete mpsiTmodel; }
An exception is raised with a fault as soon as the line:
delete mptvTrecs;
Is called, why? Looks like a bug to me. I'm using Qt 5.9.2 on Windows 10 with Qt Creator 4.4.1
-
Why do you delete it at all? You properly gave a parent so no need to delete it by yourself.
And the check for != nullptr is superfluous. -
It's hard to guess what exactly is failing. I suspect 2 things: dandling pointer or, since it is a visual element, even loop might still want to handle some signals or slots for it. Try:
if (QPointer<QTableView>(mptvTrecs).isNull() == false) { mptvTrecs->deleteLater(); mptvTrecs = nullptr; }
The QPointer will make sure that object really, really is valid, and not a dangling pointer.
-
I will remove the calls to delete, is the deletion of all Qt related objects taken care of by Qt ?
-
@SPlatten said in QTableView deleting causes a fault.:
Qt related objects taken care of by Qt ?
Extract from Qt documentation =>https://doc.qt.io/qt-5/objecttrees.html
When you create a QObject with another object as parent, it's added to the parent's children() list, and is deleted when the parent is.
-
@KroMignon , if I create an instance of a timer using new where no parent is specified, am I responsible for deleting the timer when I'm done with it?
-
@SPlatten said in QTableView deleting causes a fault.:
if I create an instance of a timer using new where no parent is specified, am I responsible for deleting the timer when I'm done with it?
When you create a QObject with another object as parent, it's added to the parent's children() list, and is deleted when the parent is.
Isn't this obvious enough?
-
@Christian-Ehrlicher , if a widget is added to a layout using append, does this also mean it will be deleted by the layout?
-
@SPlatten said in QTableView deleting causes a fault.:
does this also mean it will be deleted by the layout?
Not by the layout but by the widget where the layout is set to since the added widget gets re-parented to them.
-
@SPlatten said in QTableView deleting causes a fault.:
if I create an instance of a timer using new where no parent is specified, am I responsible for deleting the timer when I'm done with it?
You can delete the timer when ever you want (but I will recommend to use
deleteLater()
to ensure it will be destroyed in this working thread).
If you do not delete it, it will be delete during parent destructor.I am just paraphrasing what is already written in documentation (link from my previous post).
-
@SPlatten Deleting a widget should work as well if it is not used anymore. But if crash is triggered by doing so, something is wrong. Sometimes, you may need to figure out why since it could be critical.