<?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[std::ofstream invalid write of size 4]]></title><description><![CDATA[<p>Hi Leute,<br />
ich hab hier etwas was ich mir nicht erklären kann. Ich hab mir eine Fehler Klasse geschrieben aber das klappt noch nicht so wie ich mir das vorstelle.</p>
<p>Die .log wird noch beschrieben danach geht nichts mehr.</p>
<p>Hier mein Code:</p>
<pre><code>try {
      foo();
    }
    catch (Err&amp; excep) {
      excep.say();
      return;
    }
</code></pre>
<p>Error Klasse:</p>
<pre><code>#ifndef ERR_H
#define ERR_H

#include &lt;string&gt;
#include &lt;cstring&gt;
#include &lt;ctime&gt;
#include &lt;cstdio&gt;
#include &lt;exception&gt;
#include &lt;iostream&gt;
#include &lt;ios&gt;
#include &lt;fstream&gt;

namespace {
	char msg[10000] = &quot;Failed: &quot;;
  size_t msglen = 0;
}

class Err : public std::exception {
public:
  Err(const std::string&amp; filename) {
    if (msglen == 0) {
      msglen = strlen(msg);
    }
    int maxRestlen = sizeof(msg) - msglen - 1;
    if (maxRestlen &gt; 0) {
      strncpy(msg+msglen, filename.c_str(), maxRestlen);
    }
  }

  /**
   * @brief Function to log the errors into err.log.
   */
  void say() {
    std::ofstream error_log;
    error_log.open(&quot;/tmp/err.log&quot;, std::ios::app);
    if (!error_log.is_open()) {
      std::cerr &lt;&lt; &quot;Error log not open!&quot; &lt;&lt; '\n';
    }

    time(&amp;Rawtime);
    Timeinfo = localtime(&amp;Rawtime);
    error_log &lt;&lt; msg &lt;&lt; ' ' &lt;&lt; asctime(Timeinfo);
  }

  /**
   * @brief Function to return the error into the shell.
   *
   * @return msg Returns the error message in msg.
   */
  const char *what() const throw() {
    return msg;
  }
private:
  time_t      Rawtime;
  struct tm * Timeinfo;
};

#endif
</code></pre>
<p>Und der Fehler aus valgrind:</p>
<pre><code>==13996== Invalid write of size 4
==13996==    at 0x595DD02: std::__basic_file&lt;char&gt;::close() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==13996==    by 0x59610D0: std::basic_filebuf&lt;char, std::char_traits&lt;char&gt; &gt;::close() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==13996==    by 0x596167C: std::basic_ofstream&lt;char, std::char_traits&lt;char&gt; &gt;::~basic_ofstream() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==13996==    by 0x41D7AF: Xqaerr::say() (xqaerr.h:43)
==13996==    by 0x41CC05: exdb::ExdbDaemon::Daemon() (exdbdaemon.cpp:109)
==13996==    by 0x4254B7: main (main.cpp:76)
==13996==  Address 0xffffffffffffffb4 is not stack'd, malloc'd or (recently) free'd
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/325021/std-ofstream-invalid-write-of-size-4</link><generator>RSS for Node</generator><lastBuildDate>Mon, 13 Jul 2026 07:58:27 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/325021.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 11 Apr 2014 10:34:06 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to std::ofstream invalid write of size 4 on Fri, 11 Apr 2014 10:34:06 GMT]]></title><description><![CDATA[<p>Hi Leute,<br />
ich hab hier etwas was ich mir nicht erklären kann. Ich hab mir eine Fehler Klasse geschrieben aber das klappt noch nicht so wie ich mir das vorstelle.</p>
<p>Die .log wird noch beschrieben danach geht nichts mehr.</p>
<p>Hier mein Code:</p>
<pre><code>try {
      foo();
    }
    catch (Err&amp; excep) {
      excep.say();
      return;
    }
</code></pre>
<p>Error Klasse:</p>
<pre><code>#ifndef ERR_H
#define ERR_H

#include &lt;string&gt;
#include &lt;cstring&gt;
#include &lt;ctime&gt;
#include &lt;cstdio&gt;
#include &lt;exception&gt;
#include &lt;iostream&gt;
#include &lt;ios&gt;
#include &lt;fstream&gt;

namespace {
	char msg[10000] = &quot;Failed: &quot;;
  size_t msglen = 0;
}

class Err : public std::exception {
public:
  Err(const std::string&amp; filename) {
    if (msglen == 0) {
      msglen = strlen(msg);
    }
    int maxRestlen = sizeof(msg) - msglen - 1;
    if (maxRestlen &gt; 0) {
      strncpy(msg+msglen, filename.c_str(), maxRestlen);
    }
  }

  /**
   * @brief Function to log the errors into err.log.
   */
  void say() {
    std::ofstream error_log;
    error_log.open(&quot;/tmp/err.log&quot;, std::ios::app);
    if (!error_log.is_open()) {
      std::cerr &lt;&lt; &quot;Error log not open!&quot; &lt;&lt; '\n';
    }

    time(&amp;Rawtime);
    Timeinfo = localtime(&amp;Rawtime);
    error_log &lt;&lt; msg &lt;&lt; ' ' &lt;&lt; asctime(Timeinfo);
  }

  /**
   * @brief Function to return the error into the shell.
   *
   * @return msg Returns the error message in msg.
   */
  const char *what() const throw() {
    return msg;
  }
private:
  time_t      Rawtime;
  struct tm * Timeinfo;
};

#endif
</code></pre>
<p>Und der Fehler aus valgrind:</p>
<pre><code>==13996== Invalid write of size 4
==13996==    at 0x595DD02: std::__basic_file&lt;char&gt;::close() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==13996==    by 0x59610D0: std::basic_filebuf&lt;char, std::char_traits&lt;char&gt; &gt;::close() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==13996==    by 0x596167C: std::basic_ofstream&lt;char, std::char_traits&lt;char&gt; &gt;::~basic_ofstream() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==13996==    by 0x41D7AF: Xqaerr::say() (xqaerr.h:43)
==13996==    by 0x41CC05: exdb::ExdbDaemon::Daemon() (exdbdaemon.cpp:109)
==13996==    by 0x4254B7: main (main.cpp:76)
==13996==  Address 0xffffffffffffffb4 is not stack'd, malloc'd or (recently) free'd
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2393995</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2393995</guid><dc:creator><![CDATA[Fuchs aus dem Wald]]></dc:creator><pubDate>Fri, 11 Apr 2014 10:34:06 GMT</pubDate></item><item><title><![CDATA[Reply to std::ofstream invalid write of size 4 on Fri, 11 Apr 2014 10:52:37 GMT]]></title><description><![CDATA[<p>Oh sry hab den throw vergessen:</p>
<pre><code>throw Err(&quot;bla bla bla&quot;);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2394003</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2394003</guid><dc:creator><![CDATA[Fuchs aus dem Wald]]></dc:creator><pubDate>Fri, 11 Apr 2014 10:52:37 GMT</pubDate></item><item><title><![CDATA[Reply to std::ofstream invalid write of size 4 on Fri, 11 Apr 2014 10:53:17 GMT]]></title><description><![CDATA[<p>Wenn error_log nicht geöffnet werden kann stürzt er ab.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2394004</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2394004</guid><dc:creator><![CDATA[nwp3]]></dc:creator><pubDate>Fri, 11 Apr 2014 10:53:17 GMT</pubDate></item><item><title><![CDATA[Reply to std::ofstream invalid write of size 4 on Fri, 11 Apr 2014 11:00:18 GMT]]></title><description><![CDATA[<p>nwp3 schrieb:</p>
<blockquote>
<p>Wenn error_log nicht geöffnet werden kann stürzt er ab.</p>
</blockquote>
<p>Warum? Es wird doch einfach nichts ins Logfile geschrieben. Das ist kein Absturz.</p>
<p>Schlimmer finde ich, wenn 2 Exception Instanzen existieren, überschreiben sie sich gegenseitig den globalen Puffer.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2394007</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2394007</guid><dc:creator><![CDATA[tntnet]]></dc:creator><pubDate>Fri, 11 Apr 2014 11:00:18 GMT</pubDate></item><item><title><![CDATA[Reply to std::ofstream invalid write of size 4 on Fri, 11 Apr 2014 11:08:21 GMT]]></title><description><![CDATA[<p>nwp3 schrieb:</p>
<blockquote>
<p>Wenn error_log nicht geöffnet werden kann stürzt er ab.</p>
</blockquote>
<p>Aber wird sie doch anscheint, sonst könnte ich den Fehler doch gar nicht in der .log nachlesen.</p>
<p>tntnet schrieb:</p>
<blockquote>
<p>nwp3 schrieb:</p>
<blockquote>
<p>Wenn error_log nicht geöffnet werden kann stürzt er ab.</p>
</blockquote>
<p>Warum? Es wird doch einfach nichts ins Logfile geschrieben. Das ist kein Absturz.</p>
<p>Schlimmer finde ich, wenn 2 Exception Instanzen existieren, überschreiben sie sich gegenseitig den globalen Puffer.</p>
</blockquote>
<p>Doch wird.</p>
<p>Ja das mit den 2 Instanzen ist richtig. Aber ist das der Fehler hier? Ich hab allerdings nur eine.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2394009</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2394009</guid><dc:creator><![CDATA[Fuchs aus dem Wald]]></dc:creator><pubDate>Fri, 11 Apr 2014 11:08:21 GMT</pubDate></item><item><title><![CDATA[Reply to std::ofstream invalid write of size 4 on Fri, 11 Apr 2014 11:26:21 GMT]]></title><description><![CDATA[<blockquote>
<p>Ich hab mir eine Fehler Klasse geschrieben aber das klappt noch nicht so wie ich mir das vorstelle.</p>
</blockquote>
<p>Tu dir selbst einen riesigen Gefallen und lösch' <s>die Scheiße</s> den Code.</p>
<p>Wie wäre es damit?</p>
<pre><code>class LogException : public std::runtime_error
{
public:
	LogException( std::string const&amp; str ) :
		std::runtime_error(str)
	{
		std::ofstream stream(&quot;exception_log.txt&quot;, std::ios::app);
		stream &lt;&lt; &quot;- LogException -\n&quot;;
		stream &lt;&lt; &quot;time since start = &quot; &lt;&lt; float(clock()) / CLOCKS_PER_SEC &lt;&lt; '\n';
		// stream &lt;&lt; &quot;time()  = &quot; &lt;&lt; time(nullptr)  &lt;&lt; '\n';
		stream &lt;&lt; &quot;Message: &quot; &lt;&lt; str &lt;&lt; '\n';
	}
};
</code></pre>
<p><code>bad_alloc</code> s kannst du dabei vorerst weglassen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2394011</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2394011</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Fri, 11 Apr 2014 11:26:21 GMT</pubDate></item><item><title><![CDATA[Reply to std::ofstream invalid write of size 4 on Fri, 11 Apr 2014 11:48:20 GMT]]></title><description><![CDATA[<p>Mit deinem Code:</p>
<pre><code>==16039== Invalid write of size 4
==16039==    at 0x595DD02: std::__basic_file&lt;char&gt;::close() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==16039==    by 0x59610D0: std::basic_filebuf&lt;char, std::char_traits&lt;char&gt; &gt;::close() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==16039==    by 0x596167C: std::basic_ofstream&lt;char, std::char_traits&lt;char&gt; &gt;::~basic_ofstream() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==16039==    by 0x41CDDD: Xqaerr::Xqaerr(std::string const&amp;) (xqaerr.h:21)
==16039==    by 0x41C6D5: exdb::ExdbDaemon::auth() (exdbdaemon.cpp:171)
==16039==    by 0x41BBDA: exdb::ExdbDaemon::Daemon() (exdbdaemon.cpp:100)
==16039==    by 0x42477B: main (main.cpp:76)
==16039==  Address 0xffffffffffffffbb is not stack'd, malloc'd or (recently) free'd
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2394018</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2394018</guid><dc:creator><![CDATA[Fuchs aus dem Wald]]></dc:creator><pubDate>Fri, 11 Apr 2014 11:48:20 GMT</pubDate></item></channel></rss>