"Undebuggable" segmentation fault
-
I wrote a simple custom editor for a
QTableView
delegate:class SignatureEditor : public QWidget { Q_OBJECT public: explicit SignatureEditor(QWidget *parent = nullptr) : QWidget(parent) { QHBoxLayout *layout = new QHBoxLayout(); _btn.setText("..."); _btn.setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding); QFont font = _btn.font(); font.setPointSize(8); _btn.setFont(font); _line.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); layout->addWidget(&_line); layout->addWidget(&_btn); this->setLayout(layout); connect(&_btn, &QToolButton::clicked, [this]() { QString filename = QFileDialog::getOpenFileName(this, tr("Select signature image"), QDir::homePath(), tr("Portable Network Graphics (*.png)")); if (!filename.isEmpty()) this->_line.setText(filename); }); } void setText(QString text) { _line.setText(text); } QString text() { return _line.text(); } void activate() { _line.setFocus(); } private: QLineEdit _line; QToolButton _btn; };
but when I close the file dialog (either pressing cancel or selecting a file) I get a segmentation fault, but the debugger doesn't show any piece of my code:
I looked into all 17 threads but none shows anything useful (only disassembled code).
Hence I don't understand neither where is my error nor how to debug this... -
hi
You have
private:
QLineEdit _line;
QToolButton _btn;which means if you put those in any layout/assign parent you would/might get double deletion as
parent will delete them (Qt) and then they are also allocated as part of the c++ clean up as they are
not pointers. and are deleted as part of the holding class destruction.
So make them pointers and new them and see if issue goes away. -
hi
You have
private:
QLineEdit _line;
QToolButton _btn;which means if you put those in any layout/assign parent you would/might get double deletion as
parent will delete them (Qt) and then they are also allocated as part of the c++ clean up as they are
not pointers. and are deleted as part of the holding class destruction.
So make them pointers and new them and see if issue goes away.@mrjj said in "Undebuggable" segmentation fault:
So make them pointers and new them and see if issue goes away.
Code updated:
class SignatureEditor : public QWidget { Q_OBJECT public: explicit SignatureEditor(QWidget *parent = nullptr) : QWidget(parent) { QHBoxLayout *layout = new QHBoxLayout(); _btn = new QToolButton(); _btn->setText("..."); _btn->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding); QFont font = _btn->font(); font.setPointSize(8); _btn->setFont(font); _line = new QLineEdit(); _line->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); layout->addWidget(_line); layout->addWidget(_btn); this->setLayout(layout); connect(_btn, &QToolButton::clicked, [this]() { QString filename = QFileDialog::getOpenFileName(this, tr("Select signature image"), QDir::homePath(), tr("Portable Network Graphics (*->png)")); if (!filename.isEmpty()) { this->_line->setText(filename); } }); } void setText(QString text) { _line->setText(text); } QString text() { return _line->text(); } void activate() { _line->setFocus(); } private: QLineEdit *_line; QToolButton *_btn; };
and here its usage:
CustomDelegateOperators::CustomDelegateOperators(QObject *parent) : QStyledItemDelegate(parent) { } QWidget *CustomDelegateOperators::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex & index ) const { switch (index.column()) { case MODEL_OPERATORS_COL_SIGNATURE: { SignatureEditor *editor = new SignatureEditor(parent); return editor; } default: return QStyledItemDelegate::createEditor(parent, option, index); } } void CustomDelegateOperators::setEditorData(QWidget *editor, const QModelIndex &index) const { switch (index.column()) { case MODEL_OPERATORS_COL_SIGNATURE: { QString value = index.model()->data(index, Qt::EditRole).toString(); SignatureEditor *widget = static_cast<SignatureEditor *>(editor); widget->setText(value); widget->activate(); break; } default: QStyledItemDelegate::setEditorData(editor, index); break; } } void CustomDelegateOperators::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &idx) const { switch (idx.column()) { case MODEL_OPERATORS_COL_SIGNATURE: { SignatureEditor *widget = static_cast<SignatureEditor *>(editor); QString value = widget->text(); // TODO: save model data break; } default: QStyledItemDelegate::setModelData(editor, model, idx); break; } }
Nothing has changed.
Another bit of information. The seg fault is due to the file dialog call. If I remove it (i.e. adding just aDebug() << "pressed"
) it works. But leaving only:QString filename = QFileDialog::getOpenFileName(this, tr("Select signature image"), QDir::homePath(), tr("Portable Network Graphics (*->png)"));
leads to the seg fault.
-
Partially fixed:
QString filename = QFileDialog::getOpenFileName(nullptr, tr("Select signature image"), QDir::homePath(), tr("Portable Network Graphics (*->png)"));
Now I can open and close the file dialog, but if I select a file the segmentation fault happens here:
this->_line->setText(filename);
it seems it doesn't like "this". But I don't know why.
Furthermore if I place a breakpoint there, it will stops, but the debugger shows no information about variables. -
Hi
Its looks ok.
however, every place you do
SignatureEditor *widget = static_cast<SignatureEditor *>(editor);
should make a check if is not null.
before using it.the error/crash you get looks like being inside invalid object. ( not liking this ptr etc )
so i would start check each cast.
SignatureEditor *widget = static_cast<SignatureEditor *>(editor); if (widget) { widget->setText(value); widget->activate(); } else qDebug() << "invalid cast for editor";
-
Hi
Its looks ok.
however, every place you do
SignatureEditor *widget = static_cast<SignatureEditor *>(editor);
should make a check if is not null.
before using it.the error/crash you get looks like being inside invalid object. ( not liking this ptr etc )
so i would start check each cast.
SignatureEditor *widget = static_cast<SignatureEditor *>(editor); if (widget) { widget->setText(value); widget->activate(); } else qDebug() << "invalid cast for editor";
-
@mrjj said in "Undebuggable" segmentation fault:
so i would start check each cast.
They return a non-zero pointer (i.e. no debug message is shown). And the address is the same among the creation of the editor and the two casts.
-
@Mark81
Hi
ok, so seems not that.
Could you try with a completely normal slot and not a lambda ? -
@mrjj said in "Undebuggable" segmentation fault:
Could you try with a completely normal slot and not a lambda ?
Ahah, you read my mind. I've already done that with the same results.
Is really so difficult to do what I'm trying to achieve?@Mark81
hi
Hmm. out of ideas then.
No, it seems very ok to have a custom widget and open a file dialog.so if it crashes at
this->_line->setText(filename);
its either "this" or _line that is not ok and we need to find out why.
i assume
this->_line->setText("test");
with no dialog also crashes ? -
@Mark81
hi
Hmm. out of ideas then.
No, it seems very ok to have a custom widget and open a file dialog.so if it crashes at
this->_line->setText(filename);
its either "this" or _line that is not ok and we need to find out why.
i assume
this->_line->setText("test");
with no dialog also crashes ?@mrjj said in "Undebuggable" segmentation fault:
i assume
this->_line->setText("test");
with no dialog also crashes ?Instead this works!
BUT:this also works:
connect(_btn, &QToolButton::clicked, [this]() { QString filename = QFileDialog::getOpenFileName(nullptr, tr("Select signature image"), QDir::homePath(), tr("Portable Network Graphics (*.png)")); });
while this crashes:
connect(_btn, &QToolButton::clicked, [this]() { QString filename = QFileDialog::getOpenFileName(nullptr, tr("Select signature image"), QDir::homePath(), tr("Portable Network Graphics (*.png)")); this->_line->setText("test"); });
-
Hi
butconnect(_btn, &QToolButton::clicked, [this]() { this->_line->setText("test"); });
does not crash? so it does seems to be dialog related ?
-
Hi
butconnect(_btn, &QToolButton::clicked, [this]() { this->_line->setText("test"); });
does not crash? so it does seems to be dialog related ?
-
@mrjj said in "Undebuggable" segmentation fault:
i assume
this->_line->setText("test");
with no dialog also crashes ?Instead this works!
BUT:this also works:
connect(_btn, &QToolButton::clicked, [this]() { QString filename = QFileDialog::getOpenFileName(nullptr, tr("Select signature image"), QDir::homePath(), tr("Portable Network Graphics (*.png)")); });
while this crashes:
connect(_btn, &QToolButton::clicked, [this]() { QString filename = QFileDialog::getOpenFileName(nullptr, tr("Select signature image"), QDir::homePath(), tr("Portable Network Graphics (*.png)")); this->_line->setText("test"); });
Does this:
connect(_btn, &QToolButton::clicked, this, [this] () -> void { QString filename = QFileDialog::getOpenFileName(nullptr, tr("Select signature image"), QDir::homePath(), tr("Portable Network Graphics (*.png)")); _line->setText("test"); });
crash?
From the behaviour described it does seem you have a dangling pointer/reference. Also don't do that:
SignatureEditor *widget = static_cast<SignatureEditor *>(editor);
without at least having a debug-time check on it:
Q_ASSERT(qobject_cast<SignatureEditor *>(editor)); SignatureEditor *widget = static_cast<SignatureEditor *>(editor);
-
Does this:
connect(_btn, &QToolButton::clicked, this, [this] () -> void { QString filename = QFileDialog::getOpenFileName(nullptr, tr("Select signature image"), QDir::homePath(), tr("Portable Network Graphics (*.png)")); _line->setText("test"); });
crash?
From the behaviour described it does seem you have a dangling pointer/reference. Also don't do that:
SignatureEditor *widget = static_cast<SignatureEditor *>(editor);
without at least having a debug-time check on it:
Q_ASSERT(qobject_cast<SignatureEditor *>(editor)); SignatureEditor *widget = static_cast<SignatureEditor *>(editor);
@kshegunov said in "Undebuggable" segmentation fault:
Does this:
connect(_btn, &QToolButton::clicked, this, [this] () -> void { QString filename = QFileDialog::getOpenFileName(nullptr, tr("Select signature image"), QDir::homePath(), tr("Portable Network Graphics (*.png)")); _line->setText("test"); });
crash?
Yes, it crashes.
From the behaviour described it does seem you have dangling pointer/reference. Also don't do that:
without at least having a debug time check on it:We've already checked the pointers and are not null.
-
@kshegunov said in "Undebuggable" segmentation fault:
Does this:
connect(_btn, &QToolButton::clicked, this, [this] () -> void { QString filename = QFileDialog::getOpenFileName(nullptr, tr("Select signature image"), QDir::homePath(), tr("Portable Network Graphics (*.png)")); _line->setText("test"); });
crash?
Yes, it crashes.
From the behaviour described it does seem you have dangling pointer/reference. Also don't do that:
without at least having a debug time check on it:We've already checked the pointers and are not null.
@Mark81 said in "Undebuggable" segmentation fault:
Yes, it crashes.
Please break before the
QLineEdit::setText
and extract the backtrace for the thread.We've already checked the pointers and are not null.
static_cast
does not check if the types involved behind the pointer are compatible or convertible, so whether the pointer's null or not is meaningless if the typing is wrong. -
Hi
Shit i read
SignatureEditor *widget = static_cast<SignatureEditor *>(editor);
as qobject_cast
so good catch, its most likely that. -
Hi
Shit i read
SignatureEditor *widget = static_cast<SignatureEditor *>(editor);
as qobject_cast
so good catch, its most likely that.@mrjj said in "Undebuggable" segmentation fault:
as qobject_cast
so good catch, its most likely that.I changed all
static_cast
toqobject_cast
with no differences.@kshegunov
here we are: -
@mrjj said in "Undebuggable" segmentation fault:
as qobject_cast
so good catch, its most likely that.I changed all
static_cast
toqobject_cast
with no differences.@kshegunov
here we are:@Mark81 said in "Undebuggable" segmentation fault:
here we are:
Two problems with the screenshot:
- I can't see the whole stack (including the root), more important. Use right click and from the context menu extract it as text please.
- You didn't specify a context object for the lambda, which I requested in my previous post.
-
@Mark81 said in "Undebuggable" segmentation fault:
here we are:
Two problems with the screenshot:
- I can't see the whole stack (including the root), more important. Use right click and from the context menu extract it as text please.
- You didn't specify a context object for the lambda, which I requested in my previous post.
@kshegunov said in "Undebuggable" segmentation fault:
Two problems with the screenshot:
- I can't see the whole stack (including the root), more important. Use right click and from the context menu extract it as text please.
- You didn't specify a context object for the lambda, which I requested in my previous post.
Sorry Sir, but we made a lot of trials. I wasn't sure about which code you want. By the way it's another syntax, very different! Why? Here the update screenshot and text:
Full backtrace attached as it's too big for the post. Here just the same info extracted as text:
1 SignatureEditor::SignatureEditor(QWidget *)::{lambda()#1}::operator()() const customdelegateoperators.h 37 0x492bb0 2 QtPrivate::FunctorCall<QtPrivate::IndexesList<>, QtPrivate::List<>, void, SignatureEditor::SignatureEditor(QWidget *)::{lambda()#1}>::call({lambda()#1}&, void * *) qobjectdefs_impl.h 128 0x48739a 3 QtPrivate::Functor<SignatureEditor::SignatureEditor(QWidget *)::{lambda()#1}, 0>::call<QtPrivate::List<>, void>({lambda()#1}&, void *, {lambda()#1}& *) qobjectdefs_impl.h 238 0x48bf44 4 QtPrivate::QFunctorSlotObject<SignatureEditor::SignatureEditor(QWidget *)::{lambda()#1}, 0, QtPrivate::List<>, void>::impl(int, QtPrivate::QSlotObjectBase *, QObject *, void * *, bool *) qobjectdefs_impl.h 421 0x48ba8d 5 QtPrivate::QSlotObjectBase::call qobjectdefs_impl.h 376 0x281c173 6 QMetaObject::activate qobject.cpp 3754 0x281c173 7 QMetaObject::activate qobject.cpp 3633 0x281c575 8 QAbstractButton::clicked moc_qabstractbutton.cpp 308 0x1b48b7ac 9 QAbstractButtonPrivate::emitClicked qabstractbutton.cpp 414 0x1b48b9d5 10 QAbstractButtonPrivate::click qabstractbutton.cpp 407 0x1b48d17a 11 QAbstractButton::mouseReleaseEvent qabstractbutton.cpp 1011 0x1b48d3aa 12 QToolButton::mouseReleaseEvent qtoolbutton.cpp 622 0x1b576fd1 13 QWidget::event qwidget.cpp 8901 0x1b3db0e0 14 QAbstractButton::event qabstractbutton.cpp 968 0x1b48e7bc 15 QToolButton::event qtoolbutton.cpp 985 0x1b57711e 16 QApplicationPrivate::notify_helper qapplication.cpp 3727 0x1b3985ca 17 QApplication::notify qapplication.cpp 3203 0x1b3a0107 18 QCoreApplication::notifyInternal2 qcoreapplication.cpp 1048 0x27f5119 19 QCoreApplication::sendSpontaneousEvent qcoreapplication.h 237 0x1b39f077 20 QApplicationPrivate::sendMouseEvent qapplication.cpp 2693 0x1b39f077 21 QWidgetWindow::handleMouseEvent qwidgetwindow.cpp 660 0x1b3f1af4 22 QWidgetWindow::event qwidgetwindow.cpp 281 0x1b3f3bf9 23 QApplicationPrivate::notify_helper qapplication.cpp 3727 0x1b3985ca 24 QApplication::notify qapplication.cpp 3099 0x1b39fb83 25 QCoreApplication::notifyInternal2 qcoreapplication.cpp 1048 0x27f5119 26 QCoreApplication::sendSpontaneousEvent qcoreapplication.h 237 0x99cabfc 27 QGuiApplicationPrivate::processMouseEvent qguiapplication.cpp 2081 0x99cabfc 28 QGuiApplicationPrivate::processWindowSystemEvent qguiapplication.cpp 1816 0x99cc4d7 29 QWindowSystemInterface::sendWindowSystemEvents qwindowsysteminterface.cpp 1032 0x99adf99 30 QWindowsGuiEventDispatcher::sendPostedEvents qwindowsguieventdispatcher.cpp 82 0x2883dae4 31 qt_internal_proc(HWND__ *, unsigned int, unsigned int, long) *16 qeventdispatcher_win.cpp 237 0x284538d 32 gapfnScSendMessage 0x769f62fa 33 ?? 0x62047e 34 USER32!GetThreadDesktop 0x769f6d3a 35 QEventDispatcherWin32Private::sendTimerEvent qeventdispatcher_win.cpp 456 0x2844dc9 36 ?? 0x62047e 37 USER32!CharPrevW 0x769f77c4 38 USER32!DispatchMessageW 0x769f788a 39 QEventDispatcherWin32::processEvents qeventdispatcher_win.cpp 629 0x2844ae8 40 QWindowsGuiEventDispatcher::processEvents qwindowsguieventdispatcher.cpp 74 0x2883dab7 41 QEventLoop::processEvents qeventloop.cpp 136 0x27f37c8 42 QEventLoop::exec qeventloop.cpp 214 0x27f3c20 43 QDialog::exec qdialog.cpp 546 0x1b5907de 44 MainWindow::on_actionImpostazioni_triggered mainwindow.cpp 1518 0x40e152 45 MainWindow::qt_static_metacall moc_mainwindow.cpp 298 0x43f973 46 MainWindow::qt_metacall moc_mainwindow.cpp 368 0x43fcc2 47 QMetaObject::metacall qmetaobject.cpp 301 0x27fe365 48 QMetaObject::activate qobject.cpp 3786 0x281c37f 49 QMetaObject::activate qobject.cpp 3633 0x281c575 50 QAction::triggered moc_qaction.cpp 376 0x1b391618 51 QAction::activate qaction.cpp 1167 0x1b394579 52 QMenuPrivate::activateCausedStack qmenu.cpp 1371 0x1b50d432 53 QMenuPrivate::activateAction qmenu.cpp 1448 0x1b51507e 54 QMenu::mouseReleaseEvent qmenu.cpp 2942 0x1b515c07 55 QWidget::event qwidget.cpp 8901 0x1b3db0e0 56 QMenu::event qmenu.cpp 3064 0x1b5180de 57 QApplicationPrivate::notify_helper qapplication.cpp 3727 0x1b3985ca 58 QApplication::notify qapplication.cpp 3203 0x1b3a0107 59 QCoreApplication::notifyInternal2 qcoreapplication.cpp 1048 0x27f5119 60 QCoreApplication::sendSpontaneousEvent qcoreapplication.h 237 0x1b39f077 61 QApplicationPrivate::sendMouseEvent qapplication.cpp 2693 0x1b39f077 62 QWidgetWindow::handleMouseEvent qwidgetwindow.cpp 556 0x1b3f0ed3 63 QWidgetWindow::event qwidgetwindow.cpp 281 0x1b3f3bf9 64 QApplicationPrivate::notify_helper qapplication.cpp 3727 0x1b3985ca 65 QApplication::notify qapplication.cpp 3099 0x1b39fb83 66 QCoreApplication::notifyInternal2 qcoreapplication.cpp 1048 0x27f5119 67 QCoreApplication::sendSpontaneousEvent qcoreapplication.h 237 0x99cabfc 68 QGuiApplicationPrivate::processMouseEvent qguiapplication.cpp 2081 0x99cabfc 69 QGuiApplicationPrivate::processWindowSystemEvent qguiapplication.cpp 1816 0x99cc4d7 70 QWindowSystemInterface::sendWindowSystemEvents qwindowsysteminterface.cpp 1032 0x99adf99 71 QWindowsGuiEventDispatcher::sendPostedEvents qwindowsguieventdispatcher.cpp 82 0x2883dae4 72 qt_internal_proc(HWND__ *, unsigned int, unsigned int, long) *16 qeventdispatcher_win.cpp 237 0x284538d 73 gapfnScSendMessage 0x769f62fa 74 ?? 0x62047e 75 USER32!GetThreadDesktop 0x769f6d3a 76 QEventDispatcherWin32Private::sendTimerEvent qeventdispatcher_win.cpp 456 0x2844dc9 77 ?? 0x62047e 78 USER32!CharPrevW 0x769f77c4 79 USER32!DispatchMessageW 0x769f788a 80 QEventDispatcherWin32::processEvents qeventdispatcher_win.cpp 629 0x2844ae8 81 QWindowsGuiEventDispatcher::processEvents qwindowsguieventdispatcher.cpp 74 0x2883dab7 82 QEventLoop::processEvents qeventloop.cpp 136 0x27f37c8 83 QEventLoop::exec qeventloop.cpp 214 0x27f3c20 84 QCoreApplication::exec qcoreapplication.cpp 1336 0x27fc30e 85 QGuiApplication::exec qguiapplication.cpp 1761 0x99c1552 86 QApplication::exec qapplication.cpp 2901 0x1b3984a9 87 qMain main.cpp 37 0x401859 88 WinMain *16 qtmain_win.cpp 104 0x447465 89 main 0x49883d [0_1548258146048_backtrace.txt](Uploading 100%)
-
Hi
Just to be 100% sure.
You still have the checks at your casts, right ?SignatureEditor *widget = qobject_cast<SignatureEditor *>(editor); // Note qobject_cast ! if (widget) { widget->setText(value); widget->activate(); } else qDebug() << "invalid cast for editor";