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

Using QTextStream leads to memory leak



  • Hi
    I'm using QTextStream for writing received data from network in a text file.
    But it leads to memory leak!
    I don't know how to solve the problem.
    when I uncomment the return, memory leak disappears.

    QVector<QFile*> fileList_rdu_1;
    QByteArray filesIDList_rdu_1;
    QTextStream logWrite;
    ***********************************
    
    void Logger::readyRead(QString frame, char ID, QString IP, QString logType)
    {
        //
        // return;
    
        if(logType == "Log_S" && ID < 24)
        {
            if(fileList_rdu_1.count() == 0)
            {
                return;
            }
            else if(filesIDList_rdu_1.contains(char(ID)))
            {
                logWrite.setDevice(fileList_rdu_1[filesIDList_rdu_1.indexOf(char(ID))]);
                logWrite << QDateTime::currentDateTime().toString("hh:mm:ss.zzz ") + frame + "Switch_IP:" + IP + " " + logType + "\n";
                //
                fileList_rdu_1[filesIDList_rdu_1.indexOf(char(ID))]->flush();
            }
        }
    }
    ****************************
    I have tried this also:
    {
            QTextStream out(fileList[filesIDList.indexOf(char(ID))]);
            out.setCodec("UTF-8");
            out << QDateTime::currentDateTime().toString("hh:mm:ss.zzz ") + frame + "Switch_IP:" + IP + " "  + logType + "\n";
            //
            fileList[filesIDList.indexOf(char(ID))]->flush();
    }
    

    thanks.



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


  • Lifetime Qt Champion

    @Jamshid How did you verify that you have a memory leak?



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


  • Lifetime Qt Champion

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



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



  • @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!



  • @Bonnie I'll try that, thanks.



  • @Bonnie Did not work!



  • Maybe I should try C++ FILE instead of QFile



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



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



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


Log in to reply