<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Using QTextStream leads to memory leak]]></title><description><![CDATA[<p dir="auto">Hi<br />
I'm using QTextStream for writing received data from network in a text file.<br />
But it leads to memory leak!<br />
I don't know how to solve the problem.<br />
when I uncomment the return, memory leak disappears.</p>
<pre><code>QVector&lt;QFile*&gt; 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" &amp;&amp; ID &lt; 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 &lt;&lt; QDateTime::currentDateTime().toString("hh:mm:ss.zzz ") + frame + "Switch_IP:" + IP + " " + logType + "\n";
            //
            fileList_rdu_1[filesIDList_rdu_1.indexOf(char(ID))]-&gt;flush();
        }
    }
}
****************************
I have tried this also:
{
        QTextStream out(fileList[filesIDList.indexOf(char(ID))]);
        out.setCodec("UTF-8");
        out &lt;&lt; QDateTime::currentDateTime().toString("hh:mm:ss.zzz ") + frame + "Switch_IP:" + IP + " "  + logType + "\n";
        //
        fileList[filesIDList.indexOf(char(ID))]-&gt;flush();
}
</code></pre>
<p dir="auto">thanks.</p>
]]></description><link>https://forum.qt.io/topic/132399/using-qtextstream-leads-to-memory-leak</link><generator>RSS for Node</generator><lastBuildDate>Sun, 12 Apr 2026 22:24:58 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/132399.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 30 Nov 2021 07:17:11 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Using QTextStream leads to memory leak on Sun, 05 Dec 2021 05:35:09 GMT]]></title><description><![CDATA[<p dir="auto">Hi<br />
Thanks <a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a> , <a class="plugin-mentions-user plugin-mentions-a" href="/user/bonnie">@<bdi>Bonnie</bdi></a> , <a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> , <a class="plugin-mentions-user plugin-mentions-a" href="/user/mranger90">@<bdi>mranger90</bdi></a></p>
<p dir="auto">I solved the problem this way:</p>
<pre><code>QVector&lt;QTextStream*&gt; textStreamList_rdu_1;

****

QTextStream *out = textStreamList_rdu_1[filesIDList_rdu_1.indexOf(char(ID))];
(*out) &lt;&lt; QDateTime::currentDateTime().toString("hh:mm:ss.zzz ") + frame + "Switch_IP:" + IP + " " + logType &lt;&lt; &amp;Qt::endl(*out);

*** // Close files and delete pointers
for(int i = 0; i &lt; fileList_rdu_1.size(); i++)
{
    fileList_rdu_1[i]-&gt;close();
   delete textStreamList_rdu_1[i];
}
//
fileList_rdu_1.clear();
textStreamList_rdu_1.clear();
</code></pre>
<p dir="auto">The main problem was when the opened file not closed immediately, the memory started to increase! was so strange.</p>
]]></description><link>https://forum.qt.io/post/693288</link><guid isPermaLink="true">https://forum.qt.io/post/693288</guid><dc:creator><![CDATA[Jamshid]]></dc:creator><pubDate>Sun, 05 Dec 2021 05:35:09 GMT</pubDate></item><item><title><![CDATA[Reply to Using QTextStream leads to memory leak on Tue, 30 Nov 2021 16:38:06 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jamshid">@<bdi>Jamshid</bdi></a> said in <a href="/post/692354">Using QTextStream leads to memory leak</a>:</p>
<blockquote>
<p dir="auto">filesIDList_rdu_1</p>
</blockquote>
<p dir="auto">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</p>
]]></description><link>https://forum.qt.io/post/692494</link><guid isPermaLink="true">https://forum.qt.io/post/692494</guid><dc:creator><![CDATA[mranger90]]></dc:creator><pubDate>Tue, 30 Nov 2021 16:38:06 GMT</pubDate></item><item><title><![CDATA[Reply to Using QTextStream leads to memory leak on Tue, 30 Nov 2021 10:02:56 GMT]]></title><description><![CDATA[<p dir="auto">I would reduce your program to a minimum to see behaviour, and then build back up from there.  For example, try nothing but <code> QTextStream out(fileList[filesIDList.indexOf(char(ID))]);</code>, don't even use that <code>out</code> variable, no actual logging, how does that behave?  If that does <em>not</em> show your memory increase, re-introduce individual lines.</p>
]]></description><link>https://forum.qt.io/post/692412</link><guid isPermaLink="true">https://forum.qt.io/post/692412</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 30 Nov 2021 10:02:56 GMT</pubDate></item><item><title><![CDATA[Reply to Using QTextStream leads to memory leak on Tue, 30 Nov 2021 09:42:16 GMT]]></title><description><![CDATA[<p dir="auto">Maybe I should try C++ FILE instead of QFile</p>
]]></description><link>https://forum.qt.io/post/692402</link><guid isPermaLink="true">https://forum.qt.io/post/692402</guid><dc:creator><![CDATA[Jamshid]]></dc:creator><pubDate>Tue, 30 Nov 2021 09:42:16 GMT</pubDate></item><item><title><![CDATA[Reply to Using QTextStream leads to memory leak on Tue, 30 Nov 2021 09:41:42 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/bonnie">@<bdi>Bonnie</bdi></a> Did not work!</p>
]]></description><link>https://forum.qt.io/post/692401</link><guid isPermaLink="true">https://forum.qt.io/post/692401</guid><dc:creator><![CDATA[Jamshid]]></dc:creator><pubDate>Tue, 30 Nov 2021 09:41:42 GMT</pubDate></item><item><title><![CDATA[Reply to Using QTextStream leads to memory leak on Tue, 30 Nov 2021 08:39:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/bonnie">@<bdi>Bonnie</bdi></a> I'll try that, thanks.</p>
]]></description><link>https://forum.qt.io/post/692378</link><guid isPermaLink="true">https://forum.qt.io/post/692378</guid><dc:creator><![CDATA[Jamshid]]></dc:creator><pubDate>Tue, 30 Nov 2021 08:39:12 GMT</pubDate></item><item><title><![CDATA[Reply to Using QTextStream leads to memory leak on Tue, 30 Nov 2021 08:37:46 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a> Writing in a normal text file.<br />
fileList_rdu_1 range is from 1 to 17.<br />
Yes you are right, I'm not checking that!</p>
]]></description><link>https://forum.qt.io/post/692377</link><guid isPermaLink="true">https://forum.qt.io/post/692377</guid><dc:creator><![CDATA[Jamshid]]></dc:creator><pubDate>Tue, 30 Nov 2021 08:37:46 GMT</pubDate></item><item><title><![CDATA[Reply to Using QTextStream leads to memory leak on Tue, 30 Nov 2021 08:18:37 GMT]]></title><description><![CDATA[<p dir="auto">How about replace the "\n" with <code>Qt::endl</code>? like</p>
<pre><code>logWrite &lt;&lt; QDateTime::currentDateTime().toString("hh:mm:ss.zzz ") &lt;&lt; frame &lt;&lt; "Switch_IP:" &lt;&lt; IP &lt;&lt; " " &lt;&lt; logType &lt;&lt; Qt::endl;
</code></pre>
<p dir="auto">That would flush the stream.</p>
]]></description><link>https://forum.qt.io/post/692373</link><guid isPermaLink="true">https://forum.qt.io/post/692373</guid><dc:creator><![CDATA[Bonnie]]></dc:creator><pubDate>Tue, 30 Nov 2021 08:18:37 GMT</pubDate></item><item><title><![CDATA[Reply to Using QTextStream leads to memory leak on Tue, 30 Nov 2021 08:09:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jamshid">@<bdi>Jamshid</bdi></a> In what kind of files are you writing? Normal files? You should use some tool to check where exactly memory is consumed.<br />
How many entries do you have in fileList_rdu_1?<br />
By the way: you are NOT checking whether filesIDList_rdu_1.indexOf(char(ID)) is out of bounds in fileList_rdu_1!</p>
]]></description><link>https://forum.qt.io/post/692369</link><guid isPermaLink="true">https://forum.qt.io/post/692369</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Tue, 30 Nov 2021 08:09:19 GMT</pubDate></item><item><title><![CDATA[Reply to Using QTextStream leads to memory leak on Tue, 30 Nov 2021 08:00:20 GMT]]></title><description><![CDATA[<p dir="auto">The RAM usage of software in Task Manager increases from 22 MB to almost 1500MB over time (a few hours) and the program crashes!</p>
]]></description><link>https://forum.qt.io/post/692365</link><guid isPermaLink="true">https://forum.qt.io/post/692365</guid><dc:creator><![CDATA[Jamshid]]></dc:creator><pubDate>Tue, 30 Nov 2021 08:00:20 GMT</pubDate></item><item><title><![CDATA[Reply to Using QTextStream leads to memory leak on Tue, 30 Nov 2021 07:26:17 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jamshid">@<bdi>Jamshid</bdi></a> How did you verify that you have a memory leak?</p>
]]></description><link>https://forum.qt.io/post/692356</link><guid isPermaLink="true">https://forum.qt.io/post/692356</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Tue, 30 Nov 2021 07:26:17 GMT</pubDate></item></channel></rss>