c++ - Why Calling exit() at Singleton's destructor is causing an infinite loop? -


calling exit() @ singleton's destructor causing infinite loop, happening singleton class , works fine elsewhere else! code :

#include <iostream> #include <stdlib.h> class singleton{       private  :       singleton(){}       public : // public destructor , allow 'delete' or whatever , not our subject       ~singleton()       {        std::cout<<"~singleton()"<<std::endl;        exit(0);                   }       static singleton * getinstance()       {         static singleton s;         return &s;           }       }; int main() {    // signleton s; , safe when constructor public     singleton::getinstance(); // enter infinate loop   return 0;    } 

output :

~singleton() ~singleton() ~singleton() ~singleton() ~singleton() ~singleton() ~singleton() ~singleton() ~singleton() ~singleton() ~singleton() ~singleton() ~singleton() ~singleton() ~singleton() ~singleton() ~singleton() ~singleton() ~singleton() .... 

that's hardly surprising.

during call exit c++ runtime calls destructors objects static storage.

if can't fix questionable design put in destructor block recursion.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -