<?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 Lebenszeit]]></title><description><![CDATA[<p>Abend,</p>
<p>sagen wir ich habe ein Meyers Singleton:</p>
<pre><code class="language-cpp">class Singleton {
public:
static Singleton&amp; Instance() {
  static Singleton theSingleton;
  return theSingleton;
}
/* more (non-static) functions here */
private:
Singleton(); // ctor hidden
Singleton(Singleton const&amp;); // copy ctor hidden
Singleton&amp; operator=(Singleton const&amp;); // assign op. hidden
~Singleton(); // dtor hidden
};
</code></pre>
<p>1. WANN wird denn die Instanz theSingleton zerstört?<br />
2. Wenn ich mehrere Singletons habe (z.b. ein Singleton App und ein Singleton Logger), kann ich dann problemlos im Dtor von App noch das Logger Singleton benutzen? Oder könnte es sein, dass das dann schon zerstört ist?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/287452/singleton-lebenszeit</link><generator>RSS for Node</generator><lastBuildDate>Thu, 20 Aug 2026 10:25:35 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/287452.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 29 May 2011 00:01:10 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Singleton Lebenszeit on Sun, 29 May 2011 00:01:10 GMT]]></title><description><![CDATA[<p>Abend,</p>
<p>sagen wir ich habe ein Meyers Singleton:</p>
<pre><code class="language-cpp">class Singleton {
public:
static Singleton&amp; Instance() {
  static Singleton theSingleton;
  return theSingleton;
}
/* more (non-static) functions here */
private:
Singleton(); // ctor hidden
Singleton(Singleton const&amp;); // copy ctor hidden
Singleton&amp; operator=(Singleton const&amp;); // assign op. hidden
~Singleton(); // dtor hidden
};
</code></pre>
<p>1. WANN wird denn die Instanz theSingleton zerstört?<br />
2. Wenn ich mehrere Singletons habe (z.b. ein Singleton App und ein Singleton Logger), kann ich dann problemlos im Dtor von App noch das Logger Singleton benutzen? Oder könnte es sein, dass das dann schon zerstört ist?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2070292</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2070292</guid><dc:creator><![CDATA[Singletonner]]></dc:creator><pubDate>Sun, 29 May 2011 00:01:10 GMT</pubDate></item><item><title><![CDATA[Reply to Singleton Lebenszeit on Sun, 29 May 2011 00:09:48 GMT]]></title><description><![CDATA[<p>Singletonner schrieb:</p>
<blockquote>
<p>1. WANN wird denn die Instanz theSingleton zerstört?</p>
</blockquote>
<p>In der geheimen Schleife nach der main(), wo die ganzen mit atexit() reservierten Dinge getan werden.</p>
<p>Singletonner schrieb:</p>
<blockquote>
<p>2. Wenn ich mehrere Singletons habe (z.b. ein Singleton App und ein Singleton Logger), kann ich dann problemlos im Dtor von App noch das Logger Singleton benutzen?</p>
</blockquote>
<p>Wenn Du den Logger schon vor der App erzeugt hast, wird er auch erst nach der App zerstört.</p>
<p>Das müßtest Du aber selber sichwerstellen. Oder den Logger zum Phönix-Singleton machen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2070294</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2070294</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 29 May 2011 00:09:48 GMT</pubDate></item><item><title><![CDATA[Reply to Singleton Lebenszeit on Sun, 29 May 2011 07:34:35 GMT]]></title><description><![CDATA[<p>@2: Statische (dazu gehört auch der Meyers Singleton) und globale Variablen werden in der umgekehrten Reihenfolge gelöscht, in der sie erzeugt wurden.</p>
<p>PS: Ein kleiner Tip vom Mod - bitte keine Themen kapern, noch dazu mit themenfremden Fragen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2070326</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2070326</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Sun, 29 May 2011 07:34:35 GMT</pubDate></item><item><title><![CDATA[Reply to Singleton Lebenszeit on Sun, 29 May 2011 07:42:13 GMT]]></title><description><![CDATA[<p>Alexandrescu hat in Modern C++ Design ein gutes Kapitel zu genau diesem Thema.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2070330</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2070330</guid><dc:creator><![CDATA[Michael E.]]></dc:creator><pubDate>Sun, 29 May 2011 07:42:13 GMT</pubDate></item><item><title><![CDATA[Reply to Singleton Lebenszeit on Sun, 29 May 2011 08:07:46 GMT]]></title><description><![CDATA[<p>Ok, mein konkretes Problem ist das:</p>
<p>Ich habe mehrere Singleton Klassen. Die wichtigsten sind eine App Klasse A und ein Logger L. Die App benutzt den Logger, d.h. man kann davon ausgehen, dass die Konstruktionsreihenfolge A, L ist. D.h. also, dass wenn ich das Singleton Design mit einer statischen lokalen Variablen mach, dass L vor A zerstört wird und ich im Dtor von A NICHT loggen kann, richtig?</p>
<p>Wäre es dann nicht die einfachste Lösung, wenn ich ein Hauptsingleton (App) habe, dass die Lebenszeit aller andren Singletons kontrolliert?<br />
Quasi so:</p>
<pre><code class="language-cpp">// Main Singleton
class App {
private:
   App();
   ~App(const App&amp; rhs);
   App&amp; operator=(const App&amp; rhs);

   static App* instance;

public:
     App&amp; createInstance() {
        if(instance == NULL)
            instance = new App();
        return *instance;
     }

     App&amp; getInstance() {
         assert(instance);
         return *instance;´
     }

     void destroyInstance() {
         if(instance)
             delete instance;
     }

     App() {
        // Andere Singletons erzeugen:
        Logger::createInstance(&quot;MyLogger&quot;);
        MemoryManager::createInstance();
        ...
     }

     ~App() {
         // Andere Singletons zerstören:
         Logger::destroyInstance();
         MemoryManager::destroyInstance();
         ...
};
</code></pre>
<p>Die Benutzung ist dann:</p>
<pre><code class="language-cpp">int main() {
    App&amp; a = App::createInstance();
    Ab jetzt ist App und alle anderen Singletons benutzbar
    a.foo();
    ...

    App::destroyInstance();  
}
</code></pre>
<p>Sieht das ok aus? Ist das ein guter Ansatz? PS: Ich benutze KEIN Multithreading.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2070332</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2070332</guid><dc:creator><![CDATA[Singletonner]]></dc:creator><pubDate>Sun, 29 May 2011 08:07:46 GMT</pubDate></item><item><title><![CDATA[Reply to Singleton Lebenszeit on Sun, 29 May 2011 12:29:42 GMT]]></title><description><![CDATA[<p>Singletonner schrieb:</p>
<blockquote>
<p>Die Benutzung ist dann:</p>
<pre><code class="language-cpp">int main() {
    App&amp; a = App::createInstance();
    Ab jetzt ist App und alle anderen Singletons benutzbar
</code></pre>
</blockquote>
<p>Nö. Der Logger muß freier sein.<br />
Wegen</p>
<pre><code class="language-cpp">struct BigLUT{
   std::map&lt;std::string,std::string&gt;;
   BigLUT(){
      ifstream in(...
      ...
   }try(std::exception ex){
      Logger::createInstance(&quot;MyLogger&quot;)&lt;&lt;&quot;Critical Error: &quot;&lt;&lt;ex.what()&lt;&lt;endl;
   };
};
BitLUT theOneAndOnlyBigLUT;
</code></pre>
<p>oder beliebig anderem Code, der vor der main ausgeführt wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2070445</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2070445</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 29 May 2011 12:29:42 GMT</pubDate></item><item><title><![CDATA[Reply to Singleton Lebenszeit on Sun, 29 May 2011 12:33:50 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/106">@volkard</a><br />
Irgendwas ist mit deinem try {} ... catch () {} schief gelaufen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2070447</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2070447</guid><dc:creator><![CDATA[FrEEzE2046]]></dc:creator><pubDate>Sun, 29 May 2011 12:33:50 GMT</pubDate></item><item><title><![CDATA[Reply to Singleton Lebenszeit on Sun, 29 May 2011 12:58:31 GMT]]></title><description><![CDATA[<p>Es gibt vor main keinen Code.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2070462</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2070462</guid><dc:creator><![CDATA[Singletonner]]></dc:creator><pubDate>Sun, 29 May 2011 12:58:31 GMT</pubDate></item><item><title><![CDATA[Reply to Singleton Lebenszeit on Sun, 29 May 2011 13:07:56 GMT]]></title><description><![CDATA[<p>Edit: gelöscht</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2070466</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2070466</guid><dc:creator><![CDATA[ipsec]]></dc:creator><pubDate>Sun, 29 May 2011 13:07:56 GMT</pubDate></item><item><title><![CDATA[Reply to Singleton Lebenszeit on Sun, 29 May 2011 13:59:18 GMT]]></title><description><![CDATA[<p>Singletonner schrieb:</p>
<blockquote>
<p>Es gibt vor main keinen Code.</p>
</blockquote>
<p>Doch, z. B. alle statischen freien Variablen. Sieh dir mal volkards Code an.</p>
<p>Edit: Ach, du bist der OP. Meinst du damit, dass in deinem konkreten Fall kein Code vor main ausgeführt wird?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2070492</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2070492</guid><dc:creator><![CDATA[Michael E.]]></dc:creator><pubDate>Sun, 29 May 2011 13:59:18 GMT</pubDate></item><item><title><![CDATA[Reply to Singleton Lebenszeit on Sun, 29 May 2011 14:03:59 GMT]]></title><description><![CDATA[<p>Michael E. schrieb:</p>
<blockquote>
<p>Edit: Ach, du bist der OP. Meinst du damit, dass in deinem konkreten Fall kein Code vor main ausgeführt wird?</p>
</blockquote>
<p>Ja, genau das wollte ich damit sagen^^<br />
Ich habe in meinem Programm keine globalen Objekte, also auch keinen Code vor main.<br />
Ich finde meinen Ansatz mit dem Singleton &quot;Manager&quot; eigentlich ganz gut. Ich weiß genau wann alle Singletons erzeugt und zerstört werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2070496</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2070496</guid><dc:creator><![CDATA[Singletonner]]></dc:creator><pubDate>Sun, 29 May 2011 14:03:59 GMT</pubDate></item><item><title><![CDATA[Reply to Singleton Lebenszeit on Sun, 29 May 2011 14:12:42 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">App() {
        Logger::createInstance(&quot;MyLogger&quot;);
        ...
     }
</code></pre>
<p>Führt eigentlich direkt zu</p>
<pre><code class="language-cpp">BigLUT(){
      Logger::createInstance(&quot;MyLogger&quot;);
      ifstream in(...
      ...
   }
App() {
        Logger::createInstance(&quot;MyLogger&quot;);
        ...
     }
</code></pre>
<p>Hmm, das könnte zyklisch werden. Angst hab.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2070502</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2070502</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 29 May 2011 14:12:42 GMT</pubDate></item><item><title><![CDATA[Reply to Singleton Lebenszeit on Sun, 29 May 2011 14:36:06 GMT]]></title><description><![CDATA[<p>Ich hab keine Ahnung was du meinst. Und was soll dieses BigLUT sein?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2070518</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2070518</guid><dc:creator><![CDATA[Singletonner]]></dc:creator><pubDate>Sun, 29 May 2011 14:36:06 GMT</pubDate></item><item><title><![CDATA[Reply to Singleton Lebenszeit on Sun, 29 May 2011 15:04:14 GMT]]></title><description><![CDATA[<p>Singletonner schrieb:</p>
<blockquote>
<p>Und was soll dieses BigLUT sein?</p>
</blockquote>
<p>Das wollte ich wissen. Du mußt Alexandrescu lesen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2070532</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2070532</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 29 May 2011 15:04:14 GMT</pubDate></item><item><title><![CDATA[Reply to Singleton Lebenszeit on Sun, 29 May 2011 21:13:15 GMT]]></title><description><![CDATA[<p>Gibt es diesen Singleton Alexandrescu Artikel irgendwo online? Ich finde ihn nirgends...</p>
<p>Ich habe mein Design jetzt etwas geändert. Meine App Klasse ist mittlerweile kein Singleton mehr. Man kann nach wie vor zwar nur 1 Instanz von ihr Erzeugen:</p>
<pre><code class="language-cpp">App* a = A::createInstance();
a-&gt;foo(); ...
// wenn fertig:
delete a;
</code></pre>
<p>aber es gibt keine globalen statischen Zugriffsfunktionen.<br />
Meine anderen Klassen wie Logger sind Meyer Singletons (also mit lokalen statischen Variablen).<br />
Frühestens im App Ctor benutze ich zum 1. Mal die Singletons:</p>
<pre><code class="language-cpp">App::App() {
   Logger::getInstance().log(&quot;App created.&quot;);
}
</code></pre>
<p>Wenn ich jetzt meine App erzeuge und kurz vor Programmende wieder lösche (delete app), dann kann ich doch im App Dtor noch die Singletons benutzen, oder?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2070717</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2070717</guid><dc:creator><![CDATA[singletonner]]></dc:creator><pubDate>Sun, 29 May 2011 21:13:15 GMT</pubDate></item><item><title><![CDATA[Reply to Singleton Lebenszeit on Mon, 30 May 2011 10:58:20 GMT]]></title><description><![CDATA[<p>Hallo,<br />
Hier sind mal zwei Artikel zu Singletons.<br />
<a href="http://www.devarticles.com/c/a/Cplusplus/C-plus-plus-In-Theory-The-Singleton-Pattern-Part-I/" rel="nofollow">http://www.devarticles.com/c/a/Cplusplus/C-plus-plus-In-Theory-The-Singleton-Pattern-Part-I/</a><br />
<a href="http://www.devarticles.com/c/a/Cplusplus/C-plus-plus-In-Theory-The-Singleton-Pattern-Part-2/" rel="nofollow">http://www.devarticles.com/c/a/Cplusplus/C-plus-plus-In-Theory-The-Singleton-Pattern-Part-2/</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2070905</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2070905</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Mon, 30 May 2011 10:58:20 GMT</pubDate></item><item><title><![CDATA[Reply to Singleton Lebenszeit on Mon, 30 May 2011 15:40:28 GMT]]></title><description><![CDATA[<p>singletonner schrieb:</p>
<blockquote>
<p>Wenn ich jetzt meine App erzeuge und kurz vor Programmende wieder lösche (delete app), dann kann ich doch im App Dtor noch die Singletons benutzen, oder?</p>
</blockquote>
<p>Eine Klasse ist erst dann &quot;erstellt&quot; wenn der Konstruktor komplett durchgelaufen ist. Daraus folgt das wenn Die Klasse B innerhalb der Konstruktors von A erzeugt wird, diese Klasse B vor A erzeugt wurde. Daraus folgt wieder das der Destruktor von B nach dem Destruktor von A aufgerufen wird (umgekehrte Reihenfolge zur Konstruktion). Also ist die Annahme richtig.</p>
<p>Will man da auf 100% sicher gehen, könnte man das über einen Smartpointer zusätzlich absichern. Über eine virtuelle Basisklasse wäre es da sogar möglich das Logging zur Laufzeit zu ändern. Ein Mechanismus der manchmal notwendig ist wenn das Logginginterface ständig zur Verfügung stehen muss, das eigentliche Logging aber vom Framework abhängig ist und nicht über die ganze Laufzeit des Programms funktioniert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2071031</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2071031</guid><dc:creator><![CDATA[tunichtgut]]></dc:creator><pubDate>Mon, 30 May 2011 15:40:28 GMT</pubDate></item></channel></rss>