<?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[Singleton-Klasse mit Microsoft Visual Studio]]></title><description><![CDATA[<p>Hallihallo!</p>
<p>Ich habe ein MFC-Projekt, in dem ich eine Singleton-Klasse brauche, da von dieser nur eine Instanz existieren soll. Das klappt auch alles soweit ganz gut, nur wenn ich die Instanz erzeugen möchte mit new(), stürzt das Programm ab. Zur Verdeutlichung hier mal der Code:</p>
<p>EinstellungenDaten.cpp</p>
<pre><code>// EinstellungenDaten.cpp: Implementierung der Klasse EinstellungenDaten.
//
//////////////////////////////////////////////////////////////////////

#include &quot;stdafx.h&quot;
#include &quot;stdafx.h&quot;
#include &quot;EinstellungenDaten.h&quot;

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
////////////////////////////////////////////////////////////////////// 

EinstellungenDaten* EinstellungenDaten::instanz = 0;

EinstellungenDaten::EinstellungenDaten()
{
	CWinApp* pApp = AfxGetApp();

	// ein Datum aus der Registry lesen
	CString wert = pApp-&gt;GetProfileString(&quot;Settings&quot;, &quot;Iterationsschritte&quot;);

	// ist schon ein Datum eingetragen?
	if(wert == NULL)
	{
		schritteMax = 1000;
		startwertTemp = 68;
	}
	else
	{
		schritteMax = atof(wert);
		startwertTemp = atoi(pApp-&gt;GetProfileString(&quot;Settings&quot;, &quot;Startwert&quot;));
	}

}

EinstellungenDaten::~EinstellungenDaten()
{
	CWinApp* pApp = AfxGetApp();
	CString wert;

	// Iterationsschritte in Registry
	wert.Format(&quot;%.0f&quot;, schritteMax);
	pApp-&gt;WriteProfileString(&quot;Settings&quot;, &quot;Iterationsschritte&quot;, wert);

	// Starttemperatur in Registry
	wert.Format(&quot;%.2f&quot;, startwertTemp);
	pApp-&gt;WriteProfileString(&quot;Settings&quot;, &quot;Iterationsschritte&quot;, wert);

}

EinstellungenDaten* EinstellungenDaten::get()
{

	if(!instanz)
	{
		instanz = new EinstellungenDaten();
	}

	return instanz;
}

int EinstellungenDaten::getSchritteMax()
{
	return schritteMax;
}

double EinstellungenDaten::getStartwertTemp()
{
	return startwertTemp;
}

void EinstellungenDaten::setVariablen(int schritte, double startwert)
{
	schritteMax = schritte;
	startwertTemp = startwert;
}
</code></pre>
<p>EinstellungenDaten.h</p>
<pre><code>// EinstellungenDaten.h: Schnittstelle für die Klasse EinstellungenDaten.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_EINSTELLUNGENDATEN_H__CE6867F4_2E5C_4C43_9FB6_8132BFA86EF0__INCLUDED_)
#define AFX_EINSTELLUNGENDATEN_H__CE6867F4_2E5C_4C43_9FB6_8132BFA86EF0__INCLUDED_

#if _MSC_VER &gt; 1000
#pragma once
#endif // _MSC_VER &gt; 1000

class EinstellungenDaten  
{
public:
	void setVariablen(int, double);
	double getStartwertTemp();
	int getSchritteMax();
	virtual ~EinstellungenDaten();
	static EinstellungenDaten* get();
private:
	EinstellungenDaten();		// privater Konstruktor
	static EinstellungenDaten* instanz;

	// in Registry gespeicherte Daten
	int schritteMax;
	double startwertTemp;
};

#endif // !defined(AFX_EINSTELLUNGENDATEN_H__CE6867F4_2E5C_4C43_9FB6_8132BFA86EF0__INCLUDED_)
</code></pre>
<p>Wenn ich nun irgendwo im Projekt EinstellungenDaten::get() aufrufe, gibt es einen Debug Assertion Failure, und zwar genau, wenn ich die Instanz mit new() erzeuge. Was mache ich falsch? Ich bin total ratlos... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/161310/singleton-klasse-mit-microsoft-visual-studio</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Jul 2026 02:07:57 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/161310.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 05 Oct 2006 12:08:13 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Singleton-Klasse mit Microsoft Visual Studio on Thu, 05 Oct 2006 12:08:13 GMT]]></title><description><![CDATA[<p>Hallihallo!</p>
<p>Ich habe ein MFC-Projekt, in dem ich eine Singleton-Klasse brauche, da von dieser nur eine Instanz existieren soll. Das klappt auch alles soweit ganz gut, nur wenn ich die Instanz erzeugen möchte mit new(), stürzt das Programm ab. Zur Verdeutlichung hier mal der Code:</p>
<p>EinstellungenDaten.cpp</p>
<pre><code>// EinstellungenDaten.cpp: Implementierung der Klasse EinstellungenDaten.
//
//////////////////////////////////////////////////////////////////////

#include &quot;stdafx.h&quot;
#include &quot;stdafx.h&quot;
#include &quot;EinstellungenDaten.h&quot;

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
////////////////////////////////////////////////////////////////////// 

EinstellungenDaten* EinstellungenDaten::instanz = 0;

EinstellungenDaten::EinstellungenDaten()
{
	CWinApp* pApp = AfxGetApp();

	// ein Datum aus der Registry lesen
	CString wert = pApp-&gt;GetProfileString(&quot;Settings&quot;, &quot;Iterationsschritte&quot;);

	// ist schon ein Datum eingetragen?
	if(wert == NULL)
	{
		schritteMax = 1000;
		startwertTemp = 68;
	}
	else
	{
		schritteMax = atof(wert);
		startwertTemp = atoi(pApp-&gt;GetProfileString(&quot;Settings&quot;, &quot;Startwert&quot;));
	}

}

EinstellungenDaten::~EinstellungenDaten()
{
	CWinApp* pApp = AfxGetApp();
	CString wert;

	// Iterationsschritte in Registry
	wert.Format(&quot;%.0f&quot;, schritteMax);
	pApp-&gt;WriteProfileString(&quot;Settings&quot;, &quot;Iterationsschritte&quot;, wert);

	// Starttemperatur in Registry
	wert.Format(&quot;%.2f&quot;, startwertTemp);
	pApp-&gt;WriteProfileString(&quot;Settings&quot;, &quot;Iterationsschritte&quot;, wert);

}

EinstellungenDaten* EinstellungenDaten::get()
{

	if(!instanz)
	{
		instanz = new EinstellungenDaten();
	}

	return instanz;
}

int EinstellungenDaten::getSchritteMax()
{
	return schritteMax;
}

double EinstellungenDaten::getStartwertTemp()
{
	return startwertTemp;
}

void EinstellungenDaten::setVariablen(int schritte, double startwert)
{
	schritteMax = schritte;
	startwertTemp = startwert;
}
</code></pre>
<p>EinstellungenDaten.h</p>
<pre><code>// EinstellungenDaten.h: Schnittstelle für die Klasse EinstellungenDaten.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_EINSTELLUNGENDATEN_H__CE6867F4_2E5C_4C43_9FB6_8132BFA86EF0__INCLUDED_)
#define AFX_EINSTELLUNGENDATEN_H__CE6867F4_2E5C_4C43_9FB6_8132BFA86EF0__INCLUDED_

#if _MSC_VER &gt; 1000
#pragma once
#endif // _MSC_VER &gt; 1000

class EinstellungenDaten  
{
public:
	void setVariablen(int, double);
	double getStartwertTemp();
	int getSchritteMax();
	virtual ~EinstellungenDaten();
	static EinstellungenDaten* get();
private:
	EinstellungenDaten();		// privater Konstruktor
	static EinstellungenDaten* instanz;

	// in Registry gespeicherte Daten
	int schritteMax;
	double startwertTemp;
};

#endif // !defined(AFX_EINSTELLUNGENDATEN_H__CE6867F4_2E5C_4C43_9FB6_8132BFA86EF0__INCLUDED_)
</code></pre>
<p>Wenn ich nun irgendwo im Projekt EinstellungenDaten::get() aufrufe, gibt es einen Debug Assertion Failure, und zwar genau, wenn ich die Instanz mit new() erzeuge. Was mache ich falsch? Ich bin total ratlos... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1149867</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1149867</guid><dc:creator><![CDATA[Lexaja]]></dc:creator><pubDate>Thu, 05 Oct 2006 12:08:13 GMT</pubDate></item><item><title><![CDATA[Reply to Singleton-Klasse mit Microsoft Visual Studio on Thu, 05 Oct 2006 12:17:29 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>Ich vermute das die Anwendung noch nicht komplett initialisiert ist.<br />
Hast Du mal einen Haltepunkt im Konstruktor des Singletons gemacht?</p>
<p>Gruß<br />
Frank</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1149880</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1149880</guid><dc:creator><![CDATA[EB]]></dc:creator><pubDate>Thu, 05 Oct 2006 12:17:29 GMT</pubDate></item><item><title><![CDATA[Reply to Singleton-Klasse mit Microsoft Visual Studio on Thu, 05 Oct 2006 12:37:18 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>deine Antwort hat mir auf Umwegen geholfen <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="😃"
    /> Hab angefangen im Konstruktor nach dem Fehler zu suchen und dabei festgestellt, dass ich die Abfrage</p>
<pre><code>if(wert == NULL)
</code></pre>
<p>lediglich in</p>
<pre><code>if(wert == &quot;&quot;)
</code></pre>
<p>ändern muss <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /></p>
<p>Vielen vielen Dank <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="😃"
    /> Ich war schon kurz vorm absoluten Verzweifeln, weil ich dachte, ich hätt wieder irgendwelche Pointer/Referenzen/Zuweisungsfehler <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1149898</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1149898</guid><dc:creator><![CDATA[Lexaja]]></dc:creator><pubDate>Thu, 05 Oct 2006 12:37:18 GMT</pubDate></item><item><title><![CDATA[Reply to Singleton-Klasse mit Microsoft Visual Studio on Thu, 05 Oct 2006 14:21:01 GMT]]></title><description><![CDATA[<p>Warum so kompliziert?</p>
<pre><code class="language-cpp">startwertTemp = pApp-&gt;GetProfileInt(&quot;Settings&quot;, &quot;Startwert&quot;,68);
</code></pre>
<p>Liefert den Wert, sofern vorhanden und 68 wenn nicht vorhanden!</p>
<p>Dennoch ist das ganze kritisch. Evtl. liefert AfxGetApp noch NULL wenn die Applikation noch nicht komplett initialisiert ist. Zudem funktioniert GetProfileString/GetProfileInt nur dann zuverlääsig und korrekt, wenn InitInstance abgelaufen ist!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1149967</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1149967</guid><dc:creator><![CDATA[Martin Richter]]></dc:creator><pubDate>Thu, 05 Oct 2006 14:21:01 GMT</pubDate></item></channel></rss>