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. Memory leak when saving a QVariantList to QSettings
Qt 6.11 is out! See what's new in the release blog

Memory leak when saving a QVariantList to QSettings

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 1.5k 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.
  • J Offline
    J Offline
    JulienMaille
    wrote on last edited by
    #1

    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
    
    A 1 Reply Last reply
    0
    • J JulienMaille

      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
      
      A Offline
      A Offline
      ambershark
      wrote on last edited by
      #2

      @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.

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      1 Reply Last reply
      1
      • J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        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.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        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