Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Heap corruption when Enumerating QActions in a QMenu in 6.8.1
Forum Updated to NodeBB v4.3 + New Features

Heap corruption when Enumerating QActions in a QMenu in 6.8.1

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 308 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bfjtg
    wrote on last edited by
    #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 items

    for (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.

    2c609e5d-14e8-49b5-80e5-f98c9f430571-image.png

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      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 * without const ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Don't mix debug and release libraries.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        2
        • hskoglundH Offline
          hskoglundH Offline
          hskoglund
          wrote on last edited by
          #4

          Maybe you got bitten by the ranged-based for loop over temp. values bug,,,

          1 Reply Last reply
          0
          • B Offline
            B Offline
            bfjtg
            wrote on last edited by
            #5

            Wow, thank you for all the quick responses. I will check these all out and report back ASAP.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              bfjtg
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved