<?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[woher kommt das memory leak]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe folgende funktion, wie kann hier valgrind ein memory leak erkennen und warum?</p>
<p>gibt es noch eine bessere, moeglichkeit einen temp filenamen zu erzeugen?<br />
in der doku steht: &quot;...it is possible that a file with that name is created by another process between the moment std::tmpnam returns and the moment this program attempts to use the returned name to create a file&quot;</p>
<pre><code>std::string build_tmp_file_name() {
    return std::tmpnam(nullptr);
}
</code></pre>
<pre><code>$ valgrind --tool=memcheck --leak-check=full ./main
==68971== Memcheck, a memory error detector
==68971== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==68971== Using Valgrind-3.12.0.SVN and LibVEX; rerun with -h for copyright info
==68971== Command: ./main
==68971== 
--68971-- run: /usr/bin/dsymutil &quot;./main&quot;
==68971== 
==68971== HEAP SUMMARY:
==68971==     in use at exit: 35,786 bytes in 429 blocks
==68971==   total heap usage: 557 allocs, 128 frees, 177,393 bytes allocated
==68971== 
==68971== 1,024 bytes in 1 blocks are definitely lost in loss record 68 of 79
==68971==    at 0x10001C44B: malloc (in /usr/local/Cellar/valgrind/HEAD/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==68971==    by 0x10033D1A2: tmpnam_buf_allocate (in /usr/lib/system/libsystem_c.dylib)
==68971==    by 0x100556AF3: __pthread_once_handler (in /usr/lib/system/libsystem_pthread.dylib)
==68971==    by 0x100545F12: _os_once (in /usr/lib/system/libsystem_platform.dylib)
==68971==    by 0x100556A92: pthread_once (in /usr/lib/system/libsystem_pthread.dylib)
==68971==    by 0x10033D142: tmpnam (in /usr/lib/system/libsystem_c.dylib)
==68971==    by 0x100001320: build_tmp_file_name() (util.cpp:43)
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/335138/woher-kommt-das-memory-leak</link><generator>RSS for Node</generator><lastBuildDate>Fri, 24 Apr 2026 21:01:42 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/335138.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 01 Nov 2015 20:41:28 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to woher kommt das memory leak on Sun, 01 Nov 2015 20:41:28 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe folgende funktion, wie kann hier valgrind ein memory leak erkennen und warum?</p>
<p>gibt es noch eine bessere, moeglichkeit einen temp filenamen zu erzeugen?<br />
in der doku steht: &quot;...it is possible that a file with that name is created by another process between the moment std::tmpnam returns and the moment this program attempts to use the returned name to create a file&quot;</p>
<pre><code>std::string build_tmp_file_name() {
    return std::tmpnam(nullptr);
}
</code></pre>
<pre><code>$ valgrind --tool=memcheck --leak-check=full ./main
==68971== Memcheck, a memory error detector
==68971== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==68971== Using Valgrind-3.12.0.SVN and LibVEX; rerun with -h for copyright info
==68971== Command: ./main
==68971== 
--68971-- run: /usr/bin/dsymutil &quot;./main&quot;
==68971== 
==68971== HEAP SUMMARY:
==68971==     in use at exit: 35,786 bytes in 429 blocks
==68971==   total heap usage: 557 allocs, 128 frees, 177,393 bytes allocated
==68971== 
==68971== 1,024 bytes in 1 blocks are definitely lost in loss record 68 of 79
==68971==    at 0x10001C44B: malloc (in /usr/local/Cellar/valgrind/HEAD/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==68971==    by 0x10033D1A2: tmpnam_buf_allocate (in /usr/lib/system/libsystem_c.dylib)
==68971==    by 0x100556AF3: __pthread_once_handler (in /usr/lib/system/libsystem_pthread.dylib)
==68971==    by 0x100545F12: _os_once (in /usr/lib/system/libsystem_platform.dylib)
==68971==    by 0x100556A92: pthread_once (in /usr/lib/system/libsystem_pthread.dylib)
==68971==    by 0x10033D142: tmpnam (in /usr/lib/system/libsystem_c.dylib)
==68971==    by 0x100001320: build_tmp_file_name() (util.cpp:43)
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2473748</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2473748</guid><dc:creator><![CDATA[HannesJ]]></dc:creator><pubDate>Sun, 01 Nov 2015 20:41:28 GMT</pubDate></item><item><title><![CDATA[Reply to woher kommt das memory leak on Sun, 01 Nov 2015 20:46:04 GMT]]></title><description><![CDATA[<p>HannesJ schrieb:</p>
<blockquote>
<p>Hallo,</p>
<p>ich habe folgende funktion, wie kann hier valgrind ein memory leak erkennen und warum?</p>
</blockquote>
<p>vermutlich ein false positive.</p>
<p>HannesJ schrieb:</p>
<blockquote>
<p>gibt es noch eine bessere, moeglichkeit einen temp filenamen zu erzeugen?</p>
</blockquote>
<p>besser ist relativ.<br />
ich verwende immer GUIDs für solche dinge.<br />
kollisionen sind da von haus aus schon sehr sehr unwahrscheinlich.</p>
<p>und natürlich kann man das kollisions-problem vermeiden, indem man die funktion die den filenamen generiert auch gleich das file erzeugen lässt. dann kann die nämlich retries machen falls es schon eine datei mit dem namen gibt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2473750</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2473750</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sun, 01 Nov 2015 20:46:04 GMT</pubDate></item><item><title><![CDATA[Reply to woher kommt das memory leak on Sun, 01 Nov 2015 20:49:13 GMT]]></title><description><![CDATA[<p>Hallo hustbaer,</p>
<p>was kann ich gegen das false positive machen, valgrind anders starten?</p>
<p>LG</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2473751</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2473751</guid><dc:creator><![CDATA[HannesJ]]></dc:creator><pubDate>Sun, 01 Nov 2015 20:49:13 GMT</pubDate></item><item><title><![CDATA[Reply to woher kommt das memory leak on Sun, 01 Nov 2015 20:51:24 GMT]]></title><description><![CDATA[<p>Wohl weil tmpnam_buf_allocate 1024 Bytes per new/malloc anlegt und nicht wieder freigibt. Darin sehe ich aber kein Problem.</p>
<pre><code>std::string build_tmp_file_name() {
    static char buf[1024];
    return std::tmpnam(buf);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2473752</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2473752</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 01 Nov 2015 20:51:24 GMT</pubDate></item><item><title><![CDATA[Reply to woher kommt das memory leak on Sun, 01 Nov 2015 20:54:20 GMT]]></title><description><![CDATA[<p>HannesJ schrieb:</p>
<blockquote>
<p>gibt es noch eine bessere, moeglichkeit einen temp filenamen zu erzeugen?<br />
in der doku steht: &quot;...it is possible that a file with that name is created by another process between the moment std::tmpnam returns and the moment this program attempts to use the returned name to create a file&quot;</p>
</blockquote>
<p>Einfach die Duku weiterlesen.</p>
<blockquote>
<p>Although tmpnam() generates names that are difficult to guess, it is nevertheless possible that<br />
between the time that tmpnam() returns a pathname, and the time that the program opens it, another<br />
program might create that pathname using open(2), or create it as a symbolic link. This can lead to<br />
security holes. To avoid such possibilities, use the open(2) O_EXCL flag to open the pathname. Or<br />
better yet, use mkstemp(3) or tmpfile(3).</p>
</blockquote>
<p>Upps, hab &quot;man tmpnam&quot; genutzt statt &quot;c++ tmpnam&quot;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2473753</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2473753</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 01 Nov 2015 20:54:20 GMT</pubDate></item><item><title><![CDATA[Reply to woher kommt das memory leak on Sun, 01 Nov 2015 21:03:41 GMT]]></title><description><![CDATA[<p>@HannesJ<br />
Es sollte aber eine Möglichkeit geben valgrind beizubringen bestimmte Allokationen zu ignorieren.<br />
In deinem Beispiel sollte es reichen valgrind beizubringen dass er alles ignorieren soll was von <code>tmpnam_buf_allocate</code> alloziert wird.</p>
<p>Da ich valgrind nie verwende (ich verwende hauptsächlich Visual Leak Detector), kann ich dir leider nicht sagen wie/wo man das eintragen/angeben kann.</p>
<p>EDIT: Hier sind suppression files dokumentiert, damit müsste das gehen: <a href="http://valgrind.org/docs/manual/mc-manual.html#mc-manual.suppfiles" rel="nofollow">http://valgrind.org/docs/manual/mc-manual.html#mc-manual.suppfiles</a></p>
<p>ps:</p>
<p>volkard schrieb:</p>
<blockquote>
<p>Wohl weil tmpnam_buf_allocate 1024 Bytes per new/malloc anlegt und nicht wieder freigibt. Darin sehe ich aber kein Problem.</p>
<pre><code>std::string build_tmp_file_name() {
    static char buf[1024];
    return std::tmpnam(buf);
}
</code></pre>
</blockquote>
<p>Ist das originale <code>tmpname</code> auch nicht threadsafe?<br />
Wenn die Funktion schon nen <code>std::string</code> zurückgibt, dann würde es ja sogar reichen einfach das <code>static</code> wegzumachen:</p>
<pre><code>std::string threadsafe_build_tmp_file_name() {
    char buf[1024];
    return std::tmpnam(buf);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2473754</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2473754</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sun, 01 Nov 2015 21:03:41 GMT</pubDate></item><item><title><![CDATA[Reply to woher kommt das memory leak on Sun, 01 Nov 2015 21:04:28 GMT]]></title><description><![CDATA[<p>oder mit boost, was mir auch ein memory leak gibt... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /><br />
ist boost version thread safe?</p>
<pre><code>std::string build_tmp_file_name() {
    return boost::lexical_cast&lt;std::string&gt;((boost::uuids::random_generator())());
}
</code></pre>
<p>volkard's version funktioniert ohne memory leak, da kein speicher am heap angelegt wird...ok...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2473755</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2473755</guid><dc:creator><![CDATA[HannesJ]]></dc:creator><pubDate>Sun, 01 Nov 2015 21:04:28 GMT</pubDate></item><item><title><![CDATA[Reply to woher kommt das memory leak on Sun, 01 Nov 2015 21:05:44 GMT]]></title><description><![CDATA[<p>hustbaer schrieb:</p>
<blockquote>
<p>Ist das originale <code>tmpname</code> auch nicht threadsafe?</p>
</blockquote>
<p>tmpname(NULL) ist nicht threadsafe und ich hab dummerweise nur das nachgebildet.</p>
<p>hustbaer schrieb:</p>
<blockquote>
<p>Wenn die Funktion schon nen std::string zurückgibt, dann würde es ja sogar reichen einfach das <code>static</code> wegzumachen:</p>
</blockquote>
<p>Ja, viel besser.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2473756</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2473756</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 01 Nov 2015 21:05:44 GMT</pubDate></item><item><title><![CDATA[Reply to woher kommt das memory leak on Sun, 01 Nov 2015 21:05:52 GMT]]></title><description><![CDATA[<p>oje:</p>
<pre><code>warning: 'tmpnam' is deprecated: This function is provided for compatibility reasons only. Due to security concerns
      inherent in the design of tmpnam(3), it is highly recommended that you use mkstemp(3) instead. [-Wdeprecated-declarations]
    return std::tmpnam(buf);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2473758</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2473758</guid><dc:creator><![CDATA[HannesJ]]></dc:creator><pubDate>Sun, 01 Nov 2015 21:05:52 GMT</pubDate></item></channel></rss>