Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Memory leak when saving a QVariantList to QSettings



  • Hi folks, I'm using this "trick" in order to save QGradientStop to settings.
    It works fine but a memory lead is detected

    QColorRampEditor::~QColorRampEditor()
    {
        QVariantList posList, colList;
        foreach(QGradientStop s, getRamp())
        {
            posList << s.first;
            colList << s.second;
        }
        settings()->setValue("Interferometry/3D/colorRampPos", posList);
        settings()->setValue("Interferometry/3D/colorRampCol", colList);
    }
    

    I tried

    foreach(QVariant v, posList ()) v.clear()
    

    but it doesn't help

    Call Stack (TID 17976):
        MSVCR120D.dll!operator new()
        c:\qt\4-head-x86-12.0\src\corelib\tools\qlist.h (372): Analysis_x86_debug.dll!QList<QVariant>::node_construct() + 0x10 bytes
        c:\qt\4-head-x86-12.0\src\corelib\tools\qlist.h (521): Analysis_x86_debug.dll!QList<QVariant>::append() + 0x10 bytes
        c:\qt\4-head-x86-12.0\src\corelib\tools\qlist.h (334): Analysis_x86_debug.dll!QList<QVariant>::operator<<() + 0x13 bytes
        d:\dev\tools\qcolorrampeditor\qcolorrampeditor.cpp (106): Analysis_x86_debug.dll!QColorRampEditor::~QColorRampEditor() + 0x23 bytes
    

  • Moderators

    @JulienMaille This doesn't seem like a real memory leak. You're not allocating anything on the heap yourself. So if it is a memory leak it would be in Qt's code.

    I've seen this before with certain MS CRT libraries and Qt's containers. I would test it to confirm it's actually a leak before really worrying about it. I'm about 90% sure it's not a valid leak.


  • Moderators

    Hi,

    foreach is actually not a very good container loop and there are valid reasons why it is marked as discarded.

    For example it always creates a copy of the container and than in your example you copy each variable again. A lot of unneeded copying instructions, nothing that should leak, mind you. But there are better options now, for example in the standard libary for(x : y) or construct a loop via container.begin and container.end etc...

    Thoses copied values should be cleaned up automatically by the OS, but you don't have control over when exactly that will happen, so chances are, memory will still be allocated when you exit your save settings function.


Log in to reply