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
Post a Comment