Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using QTextStream leads to memory leak
Qt 6.12 LTS is out! See what's new in the release blog

Using QTextStream leads to memory leak

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 2.4k Views
  • 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.
  • JamshidJ Offline
    JamshidJ Offline
    Jamshid
    wrote on last edited by Jamshid
    #3

    The RAM usage of software in Task Manager increases from 22 MB to almost 1500MB over time (a few hours) and the program crashes!

    jsulmJ 1 Reply Last reply
    0
    • JamshidJ Jamshid

      The RAM usage of software in Task Manager increases from 22 MB to almost 1500MB over time (a few hours) and the program crashes!

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @Jamshid In what kind of files are you writing? Normal files? You should use some tool to check where exactly memory is consumed.
      How many entries do you have in fileList_rdu_1?
      By the way: you are NOT checking whether filesIDList_rdu_1.indexOf(char(ID)) is out of bounds in fileList_rdu_1!

      https://forum.qt.io/topic/113070/qt-code-of-conduct

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

        How about replace the "\n" with Qt::endl? like

        logWrite << QDateTime::currentDateTime().toString("hh:mm:ss.zzz ") << frame << "Switch_IP:" << IP << " " << logType << Qt::endl;
        

        That would flush the stream.

        JamshidJ 2 Replies Last reply
        0
        • jsulmJ jsulm

          @Jamshid In what kind of files are you writing? Normal files? You should use some tool to check where exactly memory is consumed.
          How many entries do you have in fileList_rdu_1?
          By the way: you are NOT checking whether filesIDList_rdu_1.indexOf(char(ID)) is out of bounds in fileList_rdu_1!

          JamshidJ Offline
          JamshidJ Offline
          Jamshid
          wrote on last edited by
          #6

          @jsulm Writing in a normal text file.
          fileList_rdu_1 range is from 1 to 17.
          Yes you are right, I'm not checking that!

          1 Reply Last reply
          0
          • B Bonnie

            How about replace the "\n" with Qt::endl? like

            logWrite << QDateTime::currentDateTime().toString("hh:mm:ss.zzz ") << frame << "Switch_IP:" << IP << " " << logType << Qt::endl;
            

            That would flush the stream.

            JamshidJ Offline
            JamshidJ Offline
            Jamshid
            wrote on last edited by
            #7

            @Bonnie I'll try that, thanks.

            1 Reply Last reply
            0
            • B Bonnie

              How about replace the "\n" with Qt::endl? like

              logWrite << QDateTime::currentDateTime().toString("hh:mm:ss.zzz ") << frame << "Switch_IP:" << IP << " " << logType << Qt::endl;
              

              That would flush the stream.

              JamshidJ Offline
              JamshidJ Offline
              Jamshid
              wrote on last edited by
              #8

              @Bonnie Did not work!

              1 Reply Last reply
              0
              • JamshidJ Offline
                JamshidJ Offline
                Jamshid
                wrote on last edited by
                #9

                Maybe I should try C++ FILE instead of QFile

                1 Reply Last reply
                0
                • JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #10

                  I would reduce your program to a minimum to see behaviour, and then build back up from there. For example, try nothing but QTextStream out(fileList[filesIDList.indexOf(char(ID))]);, don't even use that out variable, no actual logging, how does that behave? If that does not show your memory increase, re-introduce individual lines.

                  1 Reply Last reply
                  0
                  • mranger90M Offline
                    mranger90M Offline
                    mranger90
                    wrote on last edited by
                    #11

                    @Jamshid said in Using QTextStream leads to memory leak:

                    filesIDList_rdu_1

                    One thing I'd check is to protect this with a mutex or something to make sure that you're not re-entering and causing some unexpected behavior from the setDevice

                    1 Reply Last reply
                    0
                    • JamshidJ Offline
                      JamshidJ Offline
                      Jamshid
                      wrote on last edited by
                      #12

                      Hi
                      Thanks @jsulm , @Bonnie , @JonB , @mranger90

                      I solved the problem this way:

                      QVector<QTextStream*> textStreamList_rdu_1;
                      
                      ****
                      
                      QTextStream *out = textStreamList_rdu_1[filesIDList_rdu_1.indexOf(char(ID))];
                      (*out) << QDateTime::currentDateTime().toString("hh:mm:ss.zzz ") + frame + "Switch_IP:" + IP + " " + logType << &Qt::endl(*out);
                      
                      *** // Close files and delete pointers
                      for(int i = 0; i < fileList_rdu_1.size(); i++)
                      {
                          fileList_rdu_1[i]->close();
                         delete textStreamList_rdu_1[i];
                      }
                      //
                      fileList_rdu_1.clear();
                      textStreamList_rdu_1.clear();
                      

                      The main problem was when the opened file not closed immediately, the memory started to increase! was so strange.

                      1 Reply Last reply
                      0

                      • Login

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