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 , @mranger90I 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.
-
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.
-
The RAM usage of software in Task Manager increases from 22 MB to almost 1500MB over time (a few hours) and the program crashes!
@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! -
@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? likelogWrite << QDateTime::currentDateTime().toString("hh:mm:ss.zzz ") << frame << "Switch_IP:" << IP << " " << logType << Qt::endl;That would flush the stream.
-
How about replace the "\n" with
Qt::endl? likelogWrite << QDateTime::currentDateTime().toString("hh:mm:ss.zzz ") << frame << "Switch_IP:" << IP << " " << logType << Qt::endl;That would flush the stream.
-
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 thatoutvariable, no actual logging, how does that behave? If that does not show your memory increase, re-introduce individual lines. -
Hi
Thanks @jsulm , @Bonnie , @JonB , @mranger90I 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.