Debugging issues -debugger stuck?
Hey everyone, I've recently been poking around migrating and adding features to an old project using Qt 5.14.1,
This time I'm more than sure that I introduced a memory corruption bug (due to the line where it consistently fails) but upon calling the debugger it gets stuck.
At first, launching it gets it stopped at RtlUserThreadStart, and on thread #4, however when I try to continue cdb, it launches another thread (stoping at the same place).
This keeps on going until it reaches thread number 7, when "Run requested" is seen in the status bar, and I can no longer press the continue running button.
The bottom right status message from QtCreator keeps on at 25% saying "Launching debugger".
The code where it fails leads me to think there is memory corruption do to it being:
Code:
//WidgetRenglonComprobante is a QWidget derived class who has the Q_OBJECT macro
QVector<WidgetRenglonComprobante*> renglones;
// (...)
for (auto &r : renglones)
{
//adding some error checking here and changing the for to an index oriented one, I get to see the same behaviour, at first I thought the item was out of bounds
//but that shouldn't be possible this way, and accessing the item (making a reference to it like qDebug()-ing it to see if it was null/nullptr also makes it crash
r->calculateTotal();
}
The bug is triggered whenever I remove one of the dynamically created widgets which are contained in a VBoxLayout (and the VBoxLayout is created with a scrollAreaWidgetContents parent), each widget has a 'remove button', which upon emitted, the parent (who holds the vbox and the scrollareawidget and scrollareawidgetcontents objects) catches and does:
Code:
WidgetRenglonComprobante * renglon = static_cast<WidgetRenglonComprobante*>(sender());
qDebug() << "renglon = " << renglon;
if (renglonActual == renglon)
renglonActual = nullptr;
renglones.remove(contenedorRenglones->indexOf(renglon)); // contenedorRenglones is a VBoxLayout
contenedorRenglones->removeWidget(renglon);
renglon->deleteLater();
qDebug() << "renglones.size()" << renglones.size();
The widget is removed from view correctly, however aparently renglones has some issue (QVector) and upon trying to access the remaining ones upon saving the data is where the bug is triggered -size() shows after the elimination that its been removed-.
What can I do to fix the debugger stopping? In the meantime I'll try to audit the code and look for anything I've dynamically allocated to see if I can find the issue.
Re: Debugging issues -debugger stuck?
Hi, this is just a guess, and I'm not even sure if this applies to QVector: in std::vector, when you erase an element, you invalidate all iterators pointing to the following elements. Maybe your "for (auto &r : renglones)" loop has some similar problems?
Ginsengelf
Re: Debugging issues -debugger stuck?
Quote:
Originally Posted by
Ginsengelf
Hi, this is just a guess, and I'm not even sure if this applies to QVector: in std::vector, when you erase an element, you invalidate all iterators pointing to the following elements. Maybe your "for (auto &r : renglones)" loop has some similar problems?
Ginsengelf
Yes, QVector is like std::vector: when you remove an element, all references (&r) or iterators to subsequent elements are invalidated. So a for loop (auto &r : renglones) may crash if you have previously removed elements from renglones. To be safe, use copy (auto r) or iterate by index.
Re: Debugging issues -debugger stuck?
@OmarDay: I have removed the spam URL from your signature. Spamming is forbidden on this forum. If any URL for a commercial site reappears in either your signature or in your posts, your posts will be deleted and your account will be banned.
Re: Debugging issues -debugger stuck?
Quote:
Originally Posted by
QNewbie
Hey everyone, I've recently been poking around migrating and adding features to an old project using Qt 5.14.1,
This time I'm more than sure that I introduced a memory corruption bug (due to the line where it consistently fails) but upon calling the debugger it gets stuck.
At first, launching it gets it stopped at RtlUserThreadStart, and on thread #4, however when I try to continue cdb, it launches another thread (stoping at the same place).
This keeps on going until it reaches thread number 7, when "Run requested" is seen in the status bar, and I can no longer press the continue running button.
The bottom right status message from QtCreator keeps on at 25% saying "Launching debugger".
The code where it fails leads me to think there is memory corruption do to it being:
The widget is removed from view correctly, however aparently renglones has some issue (QVector) and upon trying to access the remaining ones upon saving the data is where the bug is triggered -size() shows after the elimination that its been removed-.
What can I do to fix the debugger stopping
doodle jump? In the meantime I'll try to audit the code and look for anything I've dynamically allocated to see if I can find the issue.
You’re removing from renglones using the layout index: renglones.remove(contenedorRenglones->indexOf(renglon)). That index may not match the vector, so you end up erasing the wrong pointer and later iterate a dangling one; instead do renglones.removeOne(renglon) or renglones.remove(renglones.indexOf(renglon)), then contenedorRenglones->removeWidget(renglon); renglon->deleteLater(); and consider passing the widget pointer in the signal rather than relying on sender().