<?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[[Templates] Destruktor wird plötzlich aufgerufen]]></title><description><![CDATA[<p>Hallo, ich hab hier eine eigene Array-Klasse mithilfe von einem Template geschrieben.<br />
Jetzt hab ich nur folgendes Problem:<br />
Eine Funktion ruft den Destruktor auf. Ich hab keine Ahnung wieso...</p>
<p>Hier der Code:</p>
<p>Array.h</p>
<pre><code class="language-cpp">template&lt;class ItemClass&gt;
class CUnSafeArray
	{
public: //public methods
	//constructor
	CUnSafeArray();
	CUnSafeArray(int buffer_size);
	CUnSafeArray(int size,ItemClass init_value);
	//destructor
	~CUnSafeArray();

	ItemClass&amp; GetAt(int index){ return pArray[index]; } //UNSAFE
	ItemClass&amp; operator[](int index) { return GetAt(index); } //UNSAFE
	void SetAll(ItemClass value) { int n; for(n=0;n&lt;ArraySize;n++) pArray[n]=value; }

	bool Insert(ItemClass value, int index); //Inserts a new item at index UNSAFE
	bool InsertAtEnd(ItemClass value);

	bool Remove(int index);

	int Add(CUnSafeArray array, int index); //inserts array at index
	int Add(CUnSafeArray array); //adds array to the end of this array	

	bool Resize(int new_size);
	int GetSize() { return ArraySize; }

	void Clear();
	void ClearBuffer();

	void QuickSort(int (__cdecl *compare )(const void *, const void *)); //compare(void* item1, void* item2) is a function that returns &lt;0 if item1&lt;item2, 0 if item1==item2, &gt; if item1&gt;item2

protected: //protected methods

public: //public data
	/*none*/

protected: //protected data
	ItemClass* pArray;
	int ArraySize;
	int ArrayBufferSize;
};

//For templates, the code hast to be in the header file
//thats not a problem of C++, thats a problem of the compiler that does not support &quot;export&quot;
//So we must include here the Array.cpp file :(
#include &quot;Array.cpp&quot;
</code></pre>
<p>Hier die Funktion, die den Destruktor aufruft:</p>
<pre><code class="language-cpp">template&lt;class ItemClass&gt;
int CUnSafeArray&lt;ItemClass&gt;::Add(CUnSafeArray array, int index) //insert an array at the position index
{
/*	int n,i;
	int old_array_size=ArraySize;
	int other_array_size=array.GetSize();

	Resize(ArraySize + other_array_size); //WARNING ArraySize get's changed to the new size 

	//copy elements of this array from index on to the end
	i=index+other_array_size;
	for(n=index;n&lt;old_array_size;n++)
	{
		pArray[i]=pArray[n];
		i++;
	}

	//insert elements of the other array
	i=index;
	for(n=0; n&lt;other_array_size; n++)
	{
		pArray[i]=array.GetAt(n);
		i++;
	}
*/
	return ArraySize;
}
</code></pre>
<p>Wie ihr seht hab ich das meiste ausgekommentiert um den Fehler einzugrenzen, aber das scheint nichts zu bewirken.<br />
Wenn ich mit meinem Debugger am Ende der Funktion ankomme, wird der Destruktor aufgerufen. Dann kehrt die Funktion zurück. Und plötzlich springt das Programm wieder in den Destruktor. Der Array ist natürlich mit delete [] schon freigegeben worden -&gt; CRASH</p>
<p>Naja, hier auch nochmal main.cpp:</p>
<pre><code class="language-cpp">int main(int argc, char* argv[])
{	
	int n;

	for(n=0;n&lt;5;n++)
		test_array.InsertAtEnd(n);
	for(n=0;n&lt;4;n++)
		add_array.InsertAtEnd(n+10);

	cout &lt;&lt; &quot;Anfang: &quot; &lt;&lt;endl;
	PrintArray(test_array);

	cout &lt;&lt;endl;

	cout &lt;&lt;&quot;Füge nun folgenden Array ein: &quot; &lt;&lt;endl;
	PrintArray(add_array);

	cout &lt;&lt;&quot;füge an position 2 ein...&quot; &lt;&lt;endl;
	test_array.Add(test_array,2); // &lt;- Funktion ruft komischerweise destruktor auf
	//nach ende wird nochmals destruktor aufgerufen
	PrintArray(add_array);

	system(&quot;PAUSE&quot;);
	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/175372/templates-destruktor-wird-plötzlich-aufgerufen</link><generator>RSS for Node</generator><lastBuildDate>Sat, 19 Sep 2026 03:06:09 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/175372.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 09 Mar 2007 16:48:06 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [Templates] Destruktor wird plötzlich aufgerufen on Fri, 09 Mar 2007 16:48:06 GMT]]></title><description><![CDATA[<p>Hallo, ich hab hier eine eigene Array-Klasse mithilfe von einem Template geschrieben.<br />
Jetzt hab ich nur folgendes Problem:<br />
Eine Funktion ruft den Destruktor auf. Ich hab keine Ahnung wieso...</p>
<p>Hier der Code:</p>
<p>Array.h</p>
<pre><code class="language-cpp">template&lt;class ItemClass&gt;
class CUnSafeArray
	{
public: //public methods
	//constructor
	CUnSafeArray();
	CUnSafeArray(int buffer_size);
	CUnSafeArray(int size,ItemClass init_value);
	//destructor
	~CUnSafeArray();

	ItemClass&amp; GetAt(int index){ return pArray[index]; } //UNSAFE
	ItemClass&amp; operator[](int index) { return GetAt(index); } //UNSAFE
	void SetAll(ItemClass value) { int n; for(n=0;n&lt;ArraySize;n++) pArray[n]=value; }

	bool Insert(ItemClass value, int index); //Inserts a new item at index UNSAFE
	bool InsertAtEnd(ItemClass value);

	bool Remove(int index);

	int Add(CUnSafeArray array, int index); //inserts array at index
	int Add(CUnSafeArray array); //adds array to the end of this array	

	bool Resize(int new_size);
	int GetSize() { return ArraySize; }

	void Clear();
	void ClearBuffer();

	void QuickSort(int (__cdecl *compare )(const void *, const void *)); //compare(void* item1, void* item2) is a function that returns &lt;0 if item1&lt;item2, 0 if item1==item2, &gt; if item1&gt;item2

protected: //protected methods

public: //public data
	/*none*/

protected: //protected data
	ItemClass* pArray;
	int ArraySize;
	int ArrayBufferSize;
};

//For templates, the code hast to be in the header file
//thats not a problem of C++, thats a problem of the compiler that does not support &quot;export&quot;
//So we must include here the Array.cpp file :(
#include &quot;Array.cpp&quot;
</code></pre>
<p>Hier die Funktion, die den Destruktor aufruft:</p>
<pre><code class="language-cpp">template&lt;class ItemClass&gt;
int CUnSafeArray&lt;ItemClass&gt;::Add(CUnSafeArray array, int index) //insert an array at the position index
{
/*	int n,i;
	int old_array_size=ArraySize;
	int other_array_size=array.GetSize();

	Resize(ArraySize + other_array_size); //WARNING ArraySize get's changed to the new size 

	//copy elements of this array from index on to the end
	i=index+other_array_size;
	for(n=index;n&lt;old_array_size;n++)
	{
		pArray[i]=pArray[n];
		i++;
	}

	//insert elements of the other array
	i=index;
	for(n=0; n&lt;other_array_size; n++)
	{
		pArray[i]=array.GetAt(n);
		i++;
	}
*/
	return ArraySize;
}
</code></pre>
<p>Wie ihr seht hab ich das meiste ausgekommentiert um den Fehler einzugrenzen, aber das scheint nichts zu bewirken.<br />
Wenn ich mit meinem Debugger am Ende der Funktion ankomme, wird der Destruktor aufgerufen. Dann kehrt die Funktion zurück. Und plötzlich springt das Programm wieder in den Destruktor. Der Array ist natürlich mit delete [] schon freigegeben worden -&gt; CRASH</p>
<p>Naja, hier auch nochmal main.cpp:</p>
<pre><code class="language-cpp">int main(int argc, char* argv[])
{	
	int n;

	for(n=0;n&lt;5;n++)
		test_array.InsertAtEnd(n);
	for(n=0;n&lt;4;n++)
		add_array.InsertAtEnd(n+10);

	cout &lt;&lt; &quot;Anfang: &quot; &lt;&lt;endl;
	PrintArray(test_array);

	cout &lt;&lt;endl;

	cout &lt;&lt;&quot;Füge nun folgenden Array ein: &quot; &lt;&lt;endl;
	PrintArray(add_array);

	cout &lt;&lt;&quot;füge an position 2 ein...&quot; &lt;&lt;endl;
	test_array.Add(test_array,2); // &lt;- Funktion ruft komischerweise destruktor auf
	//nach ende wird nochmals destruktor aufgerufen
	PrintArray(add_array);

	system(&quot;PAUSE&quot;);
	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1242437</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1242437</guid><dc:creator><![CDATA[XCooperation]]></dc:creator><pubDate>Fri, 09 Mar 2007 16:48:06 GMT</pubDate></item><item><title><![CDATA[Reply to [Templates] Destruktor wird plötzlich aufgerufen on Fri, 09 Mar 2007 17:17:57 GMT]]></title><description><![CDATA[<p>Du übergibst array &quot;by value&quot;, also wird für den Funktionsaufruf eine Kopie angelegt, mit Hilfe des Copykonstruktors. Diese Kopie wird am Ende von Add wieder gelöscht -&gt; Destruktoraufruf.</p>
<p>Du hast keinen Copykonstruktor definiert, also macht das der Compiler. Und der kopiert einfach nur jeden Member, auch den Zeiger.</p>
<p>Definier für deine Klasse einen ordentlichen Copykonstruktor und Copy-Zuweisungsoperator, dann geht's auch.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1242456</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1242456</guid><dc:creator><![CDATA[MFK]]></dc:creator><pubDate>Fri, 09 Mar 2007 17:17:57 GMT</pubDate></item><item><title><![CDATA[Reply to [Templates] Destruktor wird plötzlich aufgerufen on Fri, 09 Mar 2007 19:52:17 GMT]]></title><description><![CDATA[<p>Jawoll, jetzt geht's, du hattest vollkommen Recht <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /> .<br />
Ich übergebe den Array jetzt als Referenz und nun funktionierts.</p>
<p>So, ein anständiger Copykonstruktor muss aber trotzdem her <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Danke für deine Hilfe!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1242561</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1242561</guid><dc:creator><![CDATA[XCooperation]]></dc:creator><pubDate>Fri, 09 Mar 2007 19:52:17 GMT</pubDate></item><item><title><![CDATA[Reply to [Templates] Destruktor wird plötzlich aufgerufen on Fri, 09 Mar 2007 22:39:45 GMT]]></title><description><![CDATA[<p>Übringes auch ein Zuweisungsoperator, nicht dass du da dann in die nächste Falle tappst.</p>
<p>Ich glaub irgendeiner hier hatte das mal als &quot;Dreierregel&quot; oder sowas bezeichnet:<br />
Wenn eine der drei Funktionen Kopierkonstruktor, Zuweisungsoperator oder Destruktor implementiert ist, so müssen auch die übrigen zwei implementiert werden.</p>
<p>Du kannst auch sicherheitshalber, solange du keine der beiden Funktionen nutzen willst ihre Deklaration in den private-Block schreiben, das erspart dir derartige Fehler.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1242662</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1242662</guid><dc:creator><![CDATA[.filmor]]></dc:creator><pubDate>Fri, 09 Mar 2007 22:39:45 GMT</pubDate></item><item><title><![CDATA[Reply to [Templates] Destruktor wird plötzlich aufgerufen on Sat, 10 Mar 2007 11:26:01 GMT]]></title><description><![CDATA[<p>.filmor schrieb:</p>
<blockquote>
<p>Ich glaub irgendeiner hier hatte das mal als &quot;Dreierregel&quot; oder sowas bezeichnet:</p>
</blockquote>
<p><a href="http://tutorial.schornboeck.net/bigthree.htm" rel="nofollow">http://tutorial.schornboeck.net/bigthree.htm</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1242767</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1242767</guid><dc:creator><![CDATA[THX 1138]]></dc:creator><pubDate>Sat, 10 Mar 2007 11:26:01 GMT</pubDate></item></channel></rss>