Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-118464

QBinaryJson::toBinaryData leaks ram

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 6.5.3
    • Qt5 Compat
    • None
    • Linux/Other display system

    Description

      QBinaryJson::toBinaryData appears to leak memory each time it is called. Here is my test program:

      #include <QJsonDocument>
      #include <QJsonObject>
      #include <QBinaryJson>
      #include <QCoreApplication>
      
      void test()
      {
          static const auto obj = QJsonObject{
              { QStringLiteral("hi"), QStringLiteral("there") },
              { QStringLiteral("foo"), QStringLiteral("bar") },
              { QStringLiteral("123"), QStringLiteral("456") }
          };
      
          static const auto doc = QJsonDocument{obj};
      
          const auto ba = QBinaryJson::toBinaryData(doc);
          Q_ASSERT(!ba.isEmpty());
      }
      
      int main(int argc, char* argv[])
      {
          QCoreApplication app(argc, argv);
          for (int i = 0; i < 100000; i++)
              test();
          return 0;
      } 

      and here is the valgrind report:

      $ valgrind --leak-check=full ./test
      ==1619140== Memcheck, a memory error detector
      ==1619140== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
      ==1619140== Using Valgrind-3.19.0 and LibVEX; rerun with -h for copyright info
      ==1619140== Command: ./test
      ==1619140== 
      ==1619140== 
      ==1619140== HEAP SUMMARY:
      ==1619140==     in use at exit: 8,819,404 bytes in 100,022 blocks
      ==1619140==   total heap usage: 1,200,115 allocs, 1,100,093 frees, 60,900,054 bytes allocated
      ==1619140== 
      ==1619140== 88 bytes in 1 blocks are possibly lost in loss record 18 of 25
      ==1619140==    at 0x48407B4: malloc (vg_replace_malloc.c:381)
      ==1619140==    by 0x487AAA5: ??? (in /home/hamish/dev/Qt6.5.3/6.5.3/gcc_64/lib/libQt6Core5Compat.so.6.5.3)
      ==1619140==    by 0x487C502: ??? (in /home/hamish/dev/Qt6.5.3/6.5.3/gcc_64/lib/libQt6Core5Compat.so.6.5.3)
      ==1619140==    by 0x487A58C: QBinaryJson::toRawData(QJsonDocument const&, int*) (in /home/hamish/dev/Qt6.5.3/6.5.3/gcc_64/lib/libQt6Core5Compat.so.6.5.3)
      ==1619140==    by 0x487A5ED: QBinaryJson::toBinaryData(QJsonDocument const&) (in /home/hamish/dev/Qt6.5.3/6.5.3/gcc_64/lib/libQt6Core5Compat.so.6.5.3)
      ==1619140==    by 0x109618: test() (test.cpp:17)
      ==1619140==    by 0x109816: main (test.cpp:27)
      ==1619140== 
      ==1619140== 8,799,824 bytes in 99,998 blocks are definitely lost in loss record 25 of 25
      ==1619140==    at 0x48407B4: malloc (vg_replace_malloc.c:381)
      ==1619140==    by 0x487AAA5: ??? (in /home/hamish/dev/Qt6.5.3/6.5.3/gcc_64/lib/libQt6Core5Compat.so.6.5.3)
      ==1619140==    by 0x487C502: ??? (in /home/hamish/dev/Qt6.5.3/6.5.3/gcc_64/lib/libQt6Core5Compat.so.6.5.3)
      ==1619140==    by 0x487A58C: QBinaryJson::toRawData(QJsonDocument const&, int*) (in /home/hamish/dev/Qt6.5.3/6.5.3/gcc_64/lib/libQt6Core5Compat.so.6.5.3)
      ==1619140==    by 0x487A5ED: QBinaryJson::toBinaryData(QJsonDocument const&) (in /home/hamish/dev/Qt6.5.3/6.5.3/gcc_64/lib/libQt6Core5Compat.so.6.5.3)
      ==1619140==    by 0x109618: test() (test.cpp:17)
      ==1619140==    by 0x109816: main (test.cpp:27)
      ==1619140== 
      ==1619140== LEAK SUMMARY:
      ==1619140==    definitely lost: 8,799,824 bytes in 99,998 blocks
      ==1619140==    indirectly lost: 0 bytes in 0 blocks
      ==1619140==      possibly lost: 88 bytes in 1 blocks
      ==1619140==    still reachable: 19,492 bytes in 23 blocks
      ==1619140==         suppressed: 0 bytes in 0 blocks
      ==1619140== Reachable blocks (those to which a pointer was found) are not shown.
      ==1619140== To see them, rerun with: --leak-check=full --show-leak-kinds=all
      ==1619140== 
      ==1619140== For lists of detected and suppressed errors, rerun with: -s
      ==1619140== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
      

      The amount leaked seems to be related to the size of the value to be converted. It leaks more or less if I add/remove more keys from the object. If I increase the loop iterations it leaks more proportionally.

      Attachments

        1. test.cpp
          0.6 kB
        2. test.pro
          0.1 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            sonakur Sona Kurazyan
            hmoffatt Hamish Moffatt
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes