<?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[Speicherleck? Oder falsches Kopieren?]]></title><description><![CDATA[<p>Mein Programm endet mit der Meldung &quot;terminate called after throwing an instance of 'std::bad_alloc'<br />
what(): std::bad_alloc&quot;</p>
<p>Ich versuche hier nicht allzuviel Code zu posten, aber genug um das Problem hoffentlich erkennen zu können.</p>
<p>Es gibt im Grunde genommen nur 2 relevante Klassen.<br />
Atome und Moleküle, wobei Moleküle fast nichts anderes als ein Vector von Atomen sind.</p>
<pre><code class="language-cpp">class Atom
{
protected:
    Eigen::Vector3d position;
    std::list&lt;Atom* &gt; neighbours;

public:
	Atom();
	Atom(const Atom&amp; other);
	Atom&amp; operator=(const Atom&amp; other);
	virtual ~Atom();

	virtual void setPosition3d(double, double, double);
	virtual Eigen::Vector3d getPosition3d() const;

	virtual void connect(Atom&amp; atom);
	virtual void disconnect(Atom&amp; atom);
	virtual bool isConnected(const Atom&amp; atom) const;
	virtual double distance(const Atom&amp; atom) const;
	virtual void move(double x, double y, double z);
};
</code></pre>
<pre><code class="language-cpp">Atom::Atom(const Atom&amp; other)
{
    this-&gt;position = other.position;

    for (std::list&lt;Atom*&gt;::const_iterator iter = other.neighbours.begin();
            iter != other.neighbours.end(); iter++)
    {
        connect(**iter);
    }
}

Atom&amp; Atom::operator=(const Atom&amp; other)
{
    if (this != &amp;other)
    {
        this-&gt;position = other.position;

        for (std::list&lt;Atom*&gt;::const_iterator iter = other.neighbours.begin();
                iter != other.neighbours.end(); iter++)
        {
            connect(**iter);
        }
    }
    return *this;
}

Atom::~Atom()
{
    for (std::list&lt;Atom*&gt;::iterator iter = neighbours.begin();
            iter != neighbours.end(); iter++)
    {
        (*iter)-&gt;neighbours.remove(this);
    }
}
</code></pre>
<pre><code class="language-cpp">class Molecule
{
friend class FileReader;

protected:
	std::vector&lt;Atom&gt; atoms;

public:
	Molecule();
	virtual ~Molecule();

	void addAtom(const Atom&amp;);

	const std::vector&lt;Atom&gt;&amp; getAtoms() const ;
	std::vector&lt;Atom&gt;&amp; getAtoms();
};
</code></pre>
<pre><code class="language-cpp">void Molecule::addAtom(const Atom&amp; atom)
{
    atoms.push_back(atom);
}
</code></pre>
<p>Das wären die hoffentlich relevanten Teile bisher. Nun möchte ich ein Molekül anlegen. Das soll später aus einer Datei heraus geschehen. Im Moment habe ich aber 10 Atome per Hand eingegeben:</p>
<pre><code class="language-cpp">Molecule FileReader::readFile(std::string path)
{
	Molecule m;
	if (path == &quot;&quot;)
	{
		Atom a[10];

		a[0].setPosition3d(26.091, 52.849, 39.889);
		a[1].setPosition3d(27.437, 49.969, 37.786);
		a[2].setPosition3d(24.961, 47.988, 35.671);
		a[3].setPosition3d(25.194, 44.925, 33.360);
		a[4].setPosition3d(22.428, 44.503, 30.712);
		a[5].setPosition3d(21.933, 40.811, 29.752);
		a[6].setPosition3d(19.604, 39.512, 26.973);
		a[7].setPosition3d(19.324, 37.742, 23.567);
		a[8].setPosition3d(20.108, 39.651, 20.294);
		a[9].setPosition3d(17.487, 42.426, 19.756);

		for (int i = 0; i &lt; 9; i++)
		{
			a[i].connect(a[i + 1]);
		}

		for (int i = 0; i &lt; 10; i++)
		{
			m.addAtom(a[i]);
		}
	}

	else
	{... Andere Daten}

	return m;
}
</code></pre>
<p>Das Problem schein bei Molecule.addAtom() aufzutreten. Ich verstehe die Debugger -Ausgaben leider nicht vollständig. Allerdings zeigt er mir Teile aus STL_VECTOR mit der Methode<br />
_M_allocate(size_t __n)<br />
{ return __n != 0 ? _M_impl.allocate(__n) : 0; }<br />
an.</p>
<p>Zugriffe auf<br />
atoms[i größer 0]<br />
schlagen auch fehl. Es scheinen keine Daten angelegt zu sein.</p>
<p>Würde mich freuen, wenn mir jemand sagen kann, woran ich im Moment scheitere.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/306186/speicherleck-oder-falsches-kopieren</link><generator>RSS for Node</generator><lastBuildDate>Sat, 08 Aug 2026 08:59:39 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/306186.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 20 Jul 2012 11:43:41 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Speicherleck? Oder falsches Kopieren? on Fri, 20 Jul 2012 11:43:41 GMT]]></title><description><![CDATA[<p>Mein Programm endet mit der Meldung &quot;terminate called after throwing an instance of 'std::bad_alloc'<br />
what(): std::bad_alloc&quot;</p>
<p>Ich versuche hier nicht allzuviel Code zu posten, aber genug um das Problem hoffentlich erkennen zu können.</p>
<p>Es gibt im Grunde genommen nur 2 relevante Klassen.<br />
Atome und Moleküle, wobei Moleküle fast nichts anderes als ein Vector von Atomen sind.</p>
<pre><code class="language-cpp">class Atom
{
protected:
    Eigen::Vector3d position;
    std::list&lt;Atom* &gt; neighbours;

public:
	Atom();
	Atom(const Atom&amp; other);
	Atom&amp; operator=(const Atom&amp; other);
	virtual ~Atom();

	virtual void setPosition3d(double, double, double);
	virtual Eigen::Vector3d getPosition3d() const;

	virtual void connect(Atom&amp; atom);
	virtual void disconnect(Atom&amp; atom);
	virtual bool isConnected(const Atom&amp; atom) const;
	virtual double distance(const Atom&amp; atom) const;
	virtual void move(double x, double y, double z);
};
</code></pre>
<pre><code class="language-cpp">Atom::Atom(const Atom&amp; other)
{
    this-&gt;position = other.position;

    for (std::list&lt;Atom*&gt;::const_iterator iter = other.neighbours.begin();
            iter != other.neighbours.end(); iter++)
    {
        connect(**iter);
    }
}

Atom&amp; Atom::operator=(const Atom&amp; other)
{
    if (this != &amp;other)
    {
        this-&gt;position = other.position;

        for (std::list&lt;Atom*&gt;::const_iterator iter = other.neighbours.begin();
                iter != other.neighbours.end(); iter++)
        {
            connect(**iter);
        }
    }
    return *this;
}

Atom::~Atom()
{
    for (std::list&lt;Atom*&gt;::iterator iter = neighbours.begin();
            iter != neighbours.end(); iter++)
    {
        (*iter)-&gt;neighbours.remove(this);
    }
}
</code></pre>
<pre><code class="language-cpp">class Molecule
{
friend class FileReader;

protected:
	std::vector&lt;Atom&gt; atoms;

public:
	Molecule();
	virtual ~Molecule();

	void addAtom(const Atom&amp;);

	const std::vector&lt;Atom&gt;&amp; getAtoms() const ;
	std::vector&lt;Atom&gt;&amp; getAtoms();
};
</code></pre>
<pre><code class="language-cpp">void Molecule::addAtom(const Atom&amp; atom)
{
    atoms.push_back(atom);
}
</code></pre>
<p>Das wären die hoffentlich relevanten Teile bisher. Nun möchte ich ein Molekül anlegen. Das soll später aus einer Datei heraus geschehen. Im Moment habe ich aber 10 Atome per Hand eingegeben:</p>
<pre><code class="language-cpp">Molecule FileReader::readFile(std::string path)
{
	Molecule m;
	if (path == &quot;&quot;)
	{
		Atom a[10];

		a[0].setPosition3d(26.091, 52.849, 39.889);
		a[1].setPosition3d(27.437, 49.969, 37.786);
		a[2].setPosition3d(24.961, 47.988, 35.671);
		a[3].setPosition3d(25.194, 44.925, 33.360);
		a[4].setPosition3d(22.428, 44.503, 30.712);
		a[5].setPosition3d(21.933, 40.811, 29.752);
		a[6].setPosition3d(19.604, 39.512, 26.973);
		a[7].setPosition3d(19.324, 37.742, 23.567);
		a[8].setPosition3d(20.108, 39.651, 20.294);
		a[9].setPosition3d(17.487, 42.426, 19.756);

		for (int i = 0; i &lt; 9; i++)
		{
			a[i].connect(a[i + 1]);
		}

		for (int i = 0; i &lt; 10; i++)
		{
			m.addAtom(a[i]);
		}
	}

	else
	{... Andere Daten}

	return m;
}
</code></pre>
<p>Das Problem schein bei Molecule.addAtom() aufzutreten. Ich verstehe die Debugger -Ausgaben leider nicht vollständig. Allerdings zeigt er mir Teile aus STL_VECTOR mit der Methode<br />
_M_allocate(size_t __n)<br />
{ return __n != 0 ? _M_impl.allocate(__n) : 0; }<br />
an.</p>
<p>Zugriffe auf<br />
atoms[i größer 0]<br />
schlagen auch fehl. Es scheinen keine Daten angelegt zu sein.</p>
<p>Würde mich freuen, wenn mir jemand sagen kann, woran ich im Moment scheitere.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2234346</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234346</guid><dc:creator><![CDATA[Ravenheart_unreg]]></dc:creator><pubDate>Fri, 20 Jul 2012 11:43:41 GMT</pubDate></item><item><title><![CDATA[Reply to Speicherleck? Oder falsches Kopieren? on Fri, 20 Jul 2012 12:08:51 GMT]]></title><description><![CDATA[<p>Das wird wohl nicht funktionieren:</p>
<pre><code class="language-cpp">Molecule FileReader::readFile(std::string path)
{
    Molecule m;
    if (path == &quot;&quot;)
    {
        Atom a[10];

        a[0].setPosition3d(26.091, 52.849, 39.889);
        a[1].setPosition3d(27.437, 49.969, 37.786);
        a[2].setPosition3d(24.961, 47.988, 35.671);
        a[3].setPosition3d(25.194, 44.925, 33.360);
        a[4].setPosition3d(22.428, 44.503, 30.712);
        a[5].setPosition3d(21.933, 40.811, 29.752);
        a[6].setPosition3d(19.604, 39.512, 26.973);
        a[7].setPosition3d(19.324, 37.742, 23.567);
        a[8].setPosition3d(20.108, 39.651, 20.294);
        a[9].setPosition3d(17.487, 42.426, 19.756);

        for (int i = 0; i &lt; 9; i++)
        {
            a[i].connect(a[i + 1]);
        }
</code></pre>
<p>Deine verbundenen Atome sind nämlich ein paar Zeilen später nicht mehr vorhanden, da du sie auf dem Stack angelegt hast.</p>
<p>Ob das jetzt zu deinem Fehler führt kann ich nicht sagen, der besagt jedenfalls, dass dir der Speicher ausgegangen ist. Wie viele Atome legst du denn an?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2234354</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234354</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Fri, 20 Jul 2012 12:08:51 GMT</pubDate></item><item><title><![CDATA[Reply to Speicherleck? Oder falsches Kopieren? on Fri, 20 Jul 2012 12:13:30 GMT]]></title><description><![CDATA[<p>Und warum sind die Methoden alle virtual? oO</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2234357</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234357</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Fri, 20 Jul 2012 12:13:30 GMT</pubDate></item><item><title><![CDATA[Reply to Speicherleck? Oder falsches Kopieren? on Fri, 20 Jul 2012 12:21:45 GMT]]></title><description><![CDATA[<p>Wenn du Problem mit Laufzeitfehlern hast, dann gib uns bitte auch lauffähigen Code. Siehe dritter Link in meiner Signatur.</p>
<p>Ich halte dein Design für äußerst ungünstig. Du hast ja anscheinend mehrere gleichberechtigte Kopien der Atome (warum eigentlich?), die dann aber alle verbunden sind. Das heißt, wenn Atom a und Atom b benachbart sind und du eine Kopie von a machst, dann ist b bei deinem Code auf einmal ein Nachbar sowohl von a als auch von der Kopie. Das ist bestimmt nicht gewollt!<br />
Weiterhin haben verkettete Listen nichts in performancekritischem Code verloren, wenn es in diesem Code nicht um die Stärken der Listen geht. Letzteres wird hier sicherlich der Fall sein.<br />
Es ist vermutlich auch sehr ungünstig, dass du Pointer auf Objekte in Containern hast, die eventuell Umallokieren. Dein Array ist zwar statisch, aber die Molekülklasse hat ja anscheinend einen vector und wie schon erwähnt, werden die Atomkopien im Molekül auch Nachbarn der anderen Atome. Der Vector wird beim Vergrößern aber irgendwann einmal umallokieren und die Pointer werden alle ungültig.<br />
Noch etwas: Es ist bei dir derzeit möglich, ein Atom in einem ungültigen Zustand zu erzeugen, da du ohne Not einen Defaultkonstruktor angelegt hat, der ein unvollständiges Atom erzeugt. Vermutlich weil du sonst das Array nicht hättest benutzen können. Benutze konsequent C++-Sprachmittel, dann brauchst du solche Hacks auch nicht. Jedenfalls sollte ein Konstruktor immer ein gültiges Objekt erzeugen, keine leere Hülle.<br />
Funktionen mit Namen wie &quot;distance&quot; kingen nicht nach typischen Memberfunktionen. Die Distanz zweier Objekte ist doch keine Eigenschaft eines dieser Objekte. Das klingt eher nach einem Fall für freie Funktionen.</p>
<p>Du siehst, es gibt neben dem schon genannten Fehler noch jede Menge technischer und designerischer Verbesserungmöglichkeiten, viele davon sogar dringend. Ich bin jetzt noch nicht einmal in die Tiefe des Codes eingedrungen, sondern berichte nur von meinem ersten Eindruck.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2234361</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234361</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Fri, 20 Jul 2012 12:21:45 GMT</pubDate></item><item><title><![CDATA[Reply to Speicherleck? Oder falsches Kopieren? on Fri, 20 Jul 2012 12:46:04 GMT]]></title><description><![CDATA[<p>Danke für die Antworten erstmal.</p>
<p>Ich weiß, dass das Programm einige Design-Schwächen aufweist. Meine Erfahrung in C++ ist auch nicht allzu berauschend.</p>
<p>cooky451 schrieb:</p>
<blockquote>
<p>Und warum sind die Methoden alle virtual? oO</p>
</blockquote>
<p>Ursprünglich war es geplant, dass verschiedene Dateiformate unterstützt werden, deren Atome neben den minimal erforderlichen Daten noch einige Extras speichern können.</p>
<blockquote>
<p>Du hast ja anscheinend mehrere gleichberechtigte Kopien der Atome (warum eigentlich?), die dann aber alle verbunden sind. Das heißt, wenn Atom a und Atom b benachbart sind und du eine Kopie von a machst, dann ist b bei deinem Code auf einmal ein Nachbar sowohl von a als auch von der Kopie. Das ist bestimmt nicht gewollt!</p>
</blockquote>
<p>Es ist gewollt. Mir ist leider nichts besseres eingefallen, wie ich sonst Moleküle kopieren könnte. Wenn ich A-B kopiere, wollte ich sichergehen, dass A' - B' auch verbunden sind.</p>
<p>Es ist vlt zu sagen, dass während des Programmes nur 3 Moleküle mit sagen wir 100-10000 Atomen benötigt werden.</p>
<blockquote>
<p>Es ist bei dir derzeit möglich, ein Atom in einem ungültigen Zustand zu erzeugen, da du ohne Not einen Defaultkonstruktor angelegt hat, der ein unvollständiges Atom erzeugt. Vermutlich weil du sonst das Array nicht hättest benutzen können.</p>
</blockquote>
<p>Dem habe ich bisher keine gr0ße Beachtung geschenkt.</p>
<p>Was mich jedoch am meisten interessiert ist, warum das Programm nun so abnormal terminiert. Heute morgen lief es noch und eigenltich habe ich nichts am Einlesen der Daten verändert.</p>
<blockquote>
<p>Wenn du Problem mit Laufzeitfehlern hast, dann gib uns bitte auch lauffähigen Code. Siehe dritter Link in meiner Signatur.</p>
</blockquote>
<p>Ich werde versuchen, etwas zu extrahieren. Könnte allerdings ein wenig Zeit beanspruchen.</p>
<p>Zunächst einmal vielen Dank für die Tipps bisher, auch wenn ich mich gleich im Voraus dafür entschuldige, dass ich sie nicht alle sofort beherzigen kann.<br />
Das hat nichts mit Uneinsichtigkeit zu tun, nur drängt die Zeit und ein schlecht funktionierendes Programm hilft mir erstmal mehr als ein nicht funktionierendes Programm.</p>
<p>Ich mach mich mal daran, ein lauffähiges Beispiel zusammenzustellen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2234374</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234374</guid><dc:creator><![CDATA[Ravenheart_unreg]]></dc:creator><pubDate>Fri, 20 Jul 2012 12:46:04 GMT</pubDate></item><item><title><![CDATA[Reply to Speicherleck? Oder falsches Kopieren? on Fri, 20 Jul 2012 13:04:36 GMT]]></title><description><![CDATA[<p>Ravenheart_unreg schrieb:</p>
<blockquote>
<blockquote>
<p>Du hast ja anscheinend mehrere gleichberechtigte Kopien der Atome (warum eigentlich?), die dann aber alle verbunden sind. Das heißt, wenn Atom a und Atom b benachbart sind und du eine Kopie von a machst, dann ist b bei deinem Code auf einmal ein Nachbar sowohl von a als auch von der Kopie. Das ist bestimmt nicht gewollt!</p>
</blockquote>
<p>Es ist gewollt. Mir ist leider nichts besseres eingefallen, wie ich sonst Moleküle kopieren könnte. Wenn ich A-B kopiere, wollte ich sichergehen, dass A' - B' auch verbunden sind.</p>
</blockquote>
<p>Ich glaube du verstehst den Fehler gar nicht! In dem Beispiel ist nicht nur A-B verbunden und A'-B', sondern auch A-A', A-B', A'-B und B-B'. Ich kann mir einfach nicht vorstellen, dass dies wirklich gewollt ist.</p>
<blockquote>
<p>Was mich jedoch am meisten interessiert ist, warum das Programm nun so abnormal terminiert. Heute morgen lief es noch und eigenltich habe ich nichts am Einlesen der Daten verändert.</p>
</blockquote>
<p>Ich denke, manni66 war da schon auf einer heißen Spur. Ansonsten Debugger anwerfen. Unter Linux auch mal valgrind mit allen Tests drauf loslassen. Von dem zu urteilen, was ich bisher sehe, sind da bestimmt viele Fehler drin.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2234379</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234379</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Fri, 20 Jul 2012 13:04:36 GMT</pubDate></item><item><title><![CDATA[Reply to Speicherleck? Oder falsches Kopieren? on Fri, 20 Jul 2012 13:30:36 GMT]]></title><description><![CDATA[<blockquote>
<p>Ich glaube du verstehst den Fehler gar nicht! In dem Beispiel ist nicht nur A-B verbunden und A'-B', sondern auch A-A', A-B', A'-B und B-B'. Ich kann mir einfach nicht vorstellen, dass dies wirklich gewollt ist.</p>
</blockquote>
<p>Ich weiß dass das sehr unschön ist und ich mir definitiv etwas schöneres überlegen sollte.<br />
Im Moment sollte es jedoch so sein, dass Atome, welche kopiert werden, stets temporär sind und ohnehin verfallen. Wie in diesem Beispiel A-B' .<br />
Zwar haben A' und B' noch die Adressen von A und B gespeichert, jedoch verlasse ich mich darauf, (allein das ist ja schon ein Merkmal für schlechtes Design) dass das nie eine Rolle spielen wird.</p>
<pre><code class="language-cpp">bool Atom::isConnected(const Atom&amp; atom) const
{
    for (std::list&lt;Atom*&gt;::const_iterator iter = neighbours.begin();
            iter != neighbours.end(); iter++)
    {
        if (*iter == &amp;atom)
            return true;
    }
    return false;
}
</code></pre>
<p>Solange ein Atom keine Adresse bekommt, die vorher schon ein anderes, nicht mehr existentes Atom hatte, sollte das imo funktionieren.</p>
<p>Aber ich stimme voll und ganz mit dir überein, dass das nicht optimal gelöst und sehr fehleranfällig obendrein.</p>
<p>Das Problem ist in nur in der Methode:<br />
m.addAtom(atom[i])</p>
<p>Valgrind gibt so etwas aus:<br />
==5025== Warning: silly arg (-16) to __builtin_new()</p>
<p>Im Debugger finde ich auch Hinweise auf ein ungültiges new in Vector:<br />
__gnu_cxx::new_allocator&lt;Atom&gt;::allocate() at new_allocator.h:94 0x40f479</p>
<p>Leider weiß ich dennoch nicht, wie ich diese Information auswerten kann.</p>
<p>Falls jemanden noch etwas Offensichtliches auffalen sollte, dann kann eres bitte posten.<br />
Ansonsten mache ich mich mal daran, das ganze so weit wie möglich zu verbessern.<br />
Ein etwas besseres Design hilf mir hoffentlich die meisten Fehlerquellen auszuschließen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2234389</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234389</guid><dc:creator><![CDATA[Ravenheart_unreg]]></dc:creator><pubDate>Fri, 20 Jul 2012 13:30:36 GMT</pubDate></item><item><title><![CDATA[Reply to Speicherleck? Oder falsches Kopieren? on Fri, 20 Jul 2012 14:01:58 GMT]]></title><description><![CDATA[<p>Ravenheart_unreg schrieb:</p>
<blockquote>
<p>Aber ich stimme voll und ganz mit dir überein, dass das nicht optimal gelöst und sehr fehleranfällig obendrein.</p>
</blockquote>
<p>Ravenheart_unreg schrieb:</p>
<blockquote>
<p>Solange ein Atom keine Adresse bekommt, die vorher schon ein anderes, nicht mehr existentes Atom hatte, sollte das imo funktionieren.</p>
</blockquote>
<p>Fällt dir nichts auf? Du <strong>hast</strong> Fehler.</p>
<blockquote>
<p>Das Problem ist in nur in der Methode:<br />
m.addAtom(atom[i])</p>
<p>Valgrind gibt so etwas aus:<br />
==5025== Warning: silly arg (-16) to __builtin_new()</p>
</blockquote>
<p>Da ist höchstwahrscheinlich schon lange vorher was falsch. Vermutlich existiert das vector-Objekt gar nicht mehr. Compilier mal mit Debugsymbolen (beim GCC mit -g) und starte valgrind mit (fast) allen Tests. Ich mache meistens:</p>
<pre><code>valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes ./executable
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2234398</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234398</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Fri, 20 Jul 2012 14:01:58 GMT</pubDate></item><item><title><![CDATA[Reply to Speicherleck? Oder falsches Kopieren? on Fri, 20 Jul 2012 14:13:24 GMT]]></title><description><![CDATA[<blockquote>
<p>Fällt dir nichts auf? Du hast Fehler.</p>
</blockquote>
<p>Mir fällt leider nichts auf <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="😞"
    /></p>
<blockquote>
<p>Da ist höchstwahrscheinlich schon lange vorher was falsch. Vermutlich existiert das vector-Objekt gar nicht mehr. Compilier mal mit Debugsymbolen (beim GCC mit -g) und starte valgrind mit (fast) allen Tests.</p>
</blockquote>
<p>Ich kompiliere standardmäßig mit -g3</p>
<p>Ausgabe von valgrind mit den von dir gegebenen Argumenten:</p>
<pre><code>valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes ./iENM
==6301== Memcheck, a memory error detector
==6301== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==6301== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==6301== Command: ./iENM
==6301== 
Erhalte Atom: 26.091
52.849
39.889
KOPIERKTR
Erhalte Atom: 27.437
49.969
37.786
==6301== Warning: silly arg (-16) to __builtin_new()
**6301** new/new[] failed and should throw an exception, but Valgrind
**6301**    cannot throw exceptions and so is aborting instead.  Sorry.
==6301==    at 0x4C26D0C: ??? (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6301==    by 0x4C28766: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6301==    by 0x40F588: __gnu_cxx::new_allocator&lt;Atom&gt;::allocate(unsigned long, void const*) (new_allocator.h:94)
==6301==    by 0x40F0D8: std::_Vector_base&lt;Atom, std::allocator&lt;Atom&gt; &gt;::_M_allocate(unsigned long) (in /home/anubis/Coding/workspace eclipse c++/iENM/Debug/iENM)
==6301==    by 0x43BE3B: void std::vector&lt;Atom, std::allocator&lt;Atom&gt; &gt;::_M_emplace_back_aux&lt;Atom const&amp;&gt;(Atom const&amp;&amp;&amp;) (vector.tcc:402)
==6301==    by 0x43BAD2: std::vector&lt;Atom, std::allocator&lt;Atom&gt; &gt;::push_back(Atom const&amp;) (stl_vector.h:891)
==6301==    by 0x43B635: Molecule::addAtom(Atom const&amp;) (Molecule.cpp:28)
==6301==    by 0x410778: FileReader::readFile(std::string) (FileReader.cpp:45)
==6301==    by 0x43DDD8: main (main.cpp:25)
==6301== 
==6301== FILE DESCRIPTORS: 3 open at exit.
==6301== Open file descriptor 2: /dev/pts/1
==6301==    &lt;inherited from parent&gt;
==6301== 
==6301== Open file descriptor 1: /dev/pts/1
==6301==    &lt;inherited from parent&gt;
==6301== 
==6301== Open file descriptor 0: /dev/pts/1
==6301==    &lt;inherited from parent&gt;
==6301== 
==6301== 
==6301== HEAP SUMMARY:
==6301==     in use at exit: 4,397 bytes in 18 blocks
==6301==   total heap usage: 26 allocs, 8 frees, 12,641 bytes allocated
==6301== 
==6301== 5 bytes in 1 blocks are still reachable in loss record 1 of 18
==6301==    at 0x4C28BED: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6301==    by 0x7434918: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74F56F2: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74320F7: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x400DF7F: call_init (dl-init.c:85)
==6301==    by 0x400E076: _dl_init (dl-init.c:134)
==6301==    by 0x4000B29: ??? (in /lib/x86_64-linux-gnu/ld-2.13.so)
==6301== 
==6301== 6 bytes in 1 blocks are still reachable in loss record 2 of 18
==6301==    at 0x4C28BED: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6301==    by 0x7434918: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74F57CA: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74320F7: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x400DF7F: call_init (dl-init.c:85)
==6301==    by 0x400E076: _dl_init (dl-init.c:134)
==6301==    by 0x4000B29: ??? (in /lib/x86_64-linux-gnu/ld-2.13.so)
==6301== 
==6301== 6 bytes in 1 blocks are still reachable in loss record 3 of 18
==6301==    at 0x4C28BED: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6301==    by 0x7434918: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74F58A2: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74320F7: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x400DF7F: call_init (dl-init.c:85)
==6301==    by 0x400E076: _dl_init (dl-init.c:134)
==6301==    by 0x4000B29: ??? (in /lib/x86_64-linux-gnu/ld-2.13.so)
==6301== 
==6301== 24 bytes in 1 blocks are still reachable in loss record 4 of 18
==6301==    at 0x4C28BED: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6301==    by 0x7434918: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74FC468: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74F5719: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74320F7: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x400DF7F: call_init (dl-init.c:85)
==6301==    by 0x400E076: _dl_init (dl-init.c:134)
==6301==    by 0x4000B29: ??? (in /lib/x86_64-linux-gnu/ld-2.13.so)
==6301== 
==6301== 24 bytes in 1 blocks are still reachable in loss record 5 of 18
==6301==    at 0x4C28BED: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6301==    by 0x7434918: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74FC468: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74F57F1: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74320F7: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x400DF7F: call_init (dl-init.c:85)
==6301==    by 0x400E076: _dl_init (dl-init.c:134)
==6301==    by 0x4000B29: ??? (in /lib/x86_64-linux-gnu/ld-2.13.so)
==6301== 
==6301== 24 bytes in 1 blocks are still reachable in loss record 6 of 18
==6301==    at 0x4C28BED: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6301==    by 0x7434918: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74FC468: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74F58CC: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74320F7: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x400DF7F: call_init (dl-init.c:85)
==6301==    by 0x400E076: _dl_init (dl-init.c:134)
==6301==    by 0x4000B29: ??? (in /lib/x86_64-linux-gnu/ld-2.13.so)
==6301== 
==6301== 56 bytes in 1 blocks are still reachable in loss record 7 of 18
==6301==    at 0x4C286E7: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6301==    by 0x40F588: __gnu_cxx::new_allocator&lt;Atom&gt;::allocate(unsigned long, void const*) (new_allocator.h:94)
==6301==    by 0x40F0D8: std::_Vector_base&lt;Atom, std::allocator&lt;Atom&gt; &gt;::_M_allocate(unsigned long) (in /home/anubis/Coding/workspace eclipse c++/iENM/Debug/iENM)
==6301==    by 0x43BE3B: void std::vector&lt;Atom, std::allocator&lt;Atom&gt; &gt;::_M_emplace_back_aux&lt;Atom const&amp;&gt;(Atom const&amp;&amp;&amp;) (vector.tcc:402)
==6301==    by 0x43BAD2: std::vector&lt;Atom, std::allocator&lt;Atom&gt; &gt;::push_back(Atom const&amp;) (stl_vector.h:891)
==6301==    by 0x43B635: Molecule::addAtom(Atom const&amp;) (Molecule.cpp:28)
==6301==    by 0x41075E: FileReader::readFile(std::string) (FileReader.cpp:44)
==6301==    by 0x43DDD8: main (main.cpp:25)
==6301== 
==6301== 60 bytes in 1 blocks are possibly lost in loss record 8 of 18
==6301==    at 0x4C286E7: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6301==    by 0x6710AB8: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator&lt;char&gt; const&amp;) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17)
==6301==    by 0x67124A4: char* std::string::_S_construct&lt;char const*&gt;(char const*, char const*, std::allocator&lt;char&gt; const&amp;, std::forward_iterator_tag) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17)
==6301==    by 0x6712582: std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::basic_string(char const*, std::allocator&lt;char&gt; const&amp;) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17)
==6301==    by 0x43DDC1: main (main.cpp:25)
==6301== 
==6301== 136 bytes in 1 blocks are still reachable in loss record 9 of 18
==6301==    at 0x4C28BED: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6301==    by 0x7434918: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74F5E64: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74F566B: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74320F7: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x400DF7F: call_init (dl-init.c:85)
==6301==    by 0x400E076: _dl_init (dl-init.c:134)
==6301==    by 0x4000B29: ??? (in /lib/x86_64-linux-gnu/ld-2.13.so)
==6301== 
==6301== 136 bytes in 1 blocks are still reachable in loss record 10 of 18
==6301==    at 0x4C28BED: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6301==    by 0x7434918: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74F5E64: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74F574A: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74320F7: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x400DF7F: call_init (dl-init.c:85)
==6301==    by 0x400E076: _dl_init (dl-init.c:134)
==6301==    by 0x4000B29: ??? (in /lib/x86_64-linux-gnu/ld-2.13.so)
==6301== 
==6301== 136 bytes in 1 blocks are still reachable in loss record 11 of 18
==6301==    at 0x4C28BED: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6301==    by 0x7434918: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74F5E64: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74F5822: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74320F7: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x400DF7F: call_init (dl-init.c:85)
==6301==    by 0x400E076: _dl_init (dl-init.c:134)
==6301==    by 0x4000B29: ??? (in /lib/x86_64-linux-gnu/ld-2.13.so)
==6301== 
==6301== 256 bytes in 1 blocks are still reachable in loss record 12 of 18
==6301==    at 0x4C28BED: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6301==    by 0x7434918: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74FC47A: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74F58CC: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74320F7: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x400DF7F: call_init (dl-init.c:85)
==6301==    by 0x400E076: _dl_init (dl-init.c:134)
==6301==    by 0x4000B29: ??? (in /lib/x86_64-linux-gnu/ld-2.13.so)
==6301== 
==6301== 488 bytes in 1 blocks are still reachable in loss record 13 of 18
==6301==    at 0x4C28147: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6301==    by 0x4102EB: FileReader::readFile(std::string) (FileReader.cpp:16)
==6301==    by 0x43DDD8: main (main.cpp:25)
==6301== 
==6301== 512 bytes in 1 blocks are still reachable in loss record 14 of 18
==6301==    at 0x4C28BED: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6301==    by 0x7434918: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74FC47A: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74F5719: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74320F7: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x400DF7F: call_init (dl-init.c:85)
==6301==    by 0x400E076: _dl_init (dl-init.c:134)
==6301==    by 0x4000B29: ??? (in /lib/x86_64-linux-gnu/ld-2.13.so)
==6301== 
==6301== 512 bytes in 1 blocks are still reachable in loss record 15 of 18
==6301==    at 0x4C28BED: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6301==    by 0x7434918: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74FC47A: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74F57F1: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74320F7: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x400DF7F: call_init (dl-init.c:85)
==6301==    by 0x400E076: _dl_init (dl-init.c:134)
==6301==    by 0x4000B29: ??? (in /lib/x86_64-linux-gnu/ld-2.13.so)
==6301== 
==6301== 672 bytes in 1 blocks are still reachable in loss record 16 of 18
==6301==    at 0x4C28BED: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6301==    by 0x7434918: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74F4F81: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74F5663: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74320F7: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x400DF7F: call_init (dl-init.c:85)
==6301==    by 0x400E076: _dl_init (dl-init.c:134)
==6301==    by 0x4000B29: ??? (in /lib/x86_64-linux-gnu/ld-2.13.so)
==6301== 
==6301== 672 bytes in 1 blocks are still reachable in loss record 17 of 18
==6301==    at 0x4C28BED: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6301==    by 0x7434918: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74F4F81: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74F5742: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74320F7: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x400DF7F: call_init (dl-init.c:85)
==6301==    by 0x400E076: _dl_init (dl-init.c:134)
==6301==    by 0x4000B29: ??? (in /lib/x86_64-linux-gnu/ld-2.13.so)
==6301== 
==6301== 672 bytes in 1 blocks are still reachable in loss record 18 of 18
==6301==    at 0x4C28BED: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6301==    by 0x7434918: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74F4F81: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74F581A: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x74320F7: ??? (in /usr/lib/x86_64-linux-gnu/libgfortran.so.3.0.0)
==6301==    by 0x400DF7F: call_init (dl-init.c:85)
==6301==    by 0x400E076: _dl_init (dl-init.c:134)
==6301==    by 0x4000B29: ??? (in /lib/x86_64-linux-gnu/ld-2.13.so)
==6301== 
==6301== LEAK SUMMARY:
==6301==    definitely lost: 0 bytes in 0 blocks
==6301==    indirectly lost: 0 bytes in 0 blocks
==6301==      possibly lost: 60 bytes in 1 blocks
==6301==    still reachable: 4,337 bytes in 17 blocks
==6301==         suppressed: 0 bytes in 0 blocks
==6301== 
==6301== For counts of detected and suppressed errors, rerun with: -v
==6301== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)
</code></pre>
<p>Als Laie kann ich nur herauslesen, dass eine Speicherallokation für einen Alllokator fehlschlägt.</p>
<p>Ich könnte um den Abbruch von valgrnd zu verhindern, eventuell die Tipps von<br />
<a href="http://stackoverflow.com/questions/365458/how-can-i-detect-if-a-program-is-running-from-within-valgrind" rel="nofollow">http://stackoverflow.com/questions/365458/how-can-i-detect-if-a-program-is-running-from-within-valgrind</a> befolgen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2234402</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234402</guid><dc:creator><![CDATA[Ravenheart_unreg]]></dc:creator><pubDate>Fri, 20 Jul 2012 14:13:24 GMT</pubDate></item><item><title><![CDATA[Reply to Speicherleck? Oder falsches Kopieren? on Fri, 20 Jul 2012 14:20:59 GMT]]></title><description><![CDATA[<p>Da bleibt mir nur der Ruf nach einem lauffähigem Beispiel, welches den Fehler zeigt, damit man sich das mal selber angucken kann.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2234403</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234403</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Fri, 20 Jul 2012 14:20:59 GMT</pubDate></item></channel></rss>