Heap corruption when Enumerating QActions in a QMenu in 6.8.1
-
I am moving an application from Qt5 in VS2019 to Qt6 in VS2022. All the code compiled with no changes. However, when I run the code, after the loop below finishes, I get a heap corruption error.
Ui::imageDisplayWindow m_ui;
QMenu *menuColor_Palette; // holds 7 menu itemsfor (auto *const palette : m_ui.menuColor_Palette->actions()) { const bool isSender = palette == sender; palette->setChecked(isSender); if (isSender) m_colorPaletteButton->setText("Palette: " + palette->text()); }
The code breaks here during clean up exiting the loop context.
~QArrayDataPointer() { if (!deref()) { (*this)->destroyAll(); free(d); } } <--break point
Not helpful, but here's the error message.
The Actions are just menu items:
QAction *actionColor_Palette_Gray QAction *actionColor_Palette_Gray_2x .... m_colorPaletteMenu->addAction(m_ui.actionColor_Palette_Gray); m_colorPaletteMenu->addAction(m_ui.actionColor_Palette_Gray_2x); ....
Note that if I comment the code inside the loop out, i.e. do nothing, I still get the heap error:
for (auto *const palette : m_ui.menuColor_Palette->actions()) { }
I have the same problem with the ~10 for() loops that iterate a QList<QAction*> list.
This code works correctly Qt 5.15.2 in VS2019
If I convert this loop to "C" style it works correctly
QList<QAction*> const ActionList = m_ui.menuColor_Palette->actions(); QAction* palette; int i; for (i = 0; i < ActionList.size(); i++) { palette = ActionList[i]; const bool isSender = palette == sender; palette->setChecked(isSender); if (isSender) m_colorPaletteButton->setText("Palette: " + palette->text()); }
Any thoughts on what is going wrong here?
It seems odd that such a straight forward for() operation would change between Qt5 and Qt6. But I am wondering if I am doing something wrong upstream. The code making the action list is pretty simple. Any help is appreciated.
-
Hi and welcome to devnet,
One difference is the implementation of QList that changed but I don't think it's the issue at hand.
What if you use just
auto *
withoutconst
? -
Don't mix debug and release libraries.
-
Maybe you got bitten by the ranged-based for loop over temp. values bug,,,
-
Christian's suggestion solved the problem. I had a mix of release and debug libraries. Made them all debug and the problem disappeared.
For the record, I did try dropping the const, but that did not help.
The "ranged-based loop over temp values" is scary and I was very much hoping that I would not need to go there.
Thanks everyone for your help, very much appreciated.