クラスA にコンストラクタ、デストラクタを定義し、クラスAにシングルトンパターンを適用すると
デストラクタが呼ばれなかったので、メソッドRelease() を追加し、その中で明示的にdelete したら
うまくデストラクタが動作しました
このように、シングルトンパターンでは明示的に _instance を delete しないとデストラクタは
呼ばれないのでしょうか?
私の考えでは、return 0; のところで クラスAのデストラクタが呼ばれると予想していたのですが
どうも違ったようでした
#include <stdio.h> class A { static A* _instance; public: A(); ~A(); static A* Instance(); void Show(); void Release(); }; A* A::_instance = 0; A* A::Instance() { if( _instance == 0 ) { _instance = new A; } return _instance; } A::A() { printf( "A construct.\n" ); } A::~A() { FILE *file = fopen( "destruct.txt", "wt" ); fputs( "destruct called.\n", file ); fclose( file ); printf( "A destruct.\n" ); } void A::Show() { printf( "A::Show call.\n" ); } void A::Release() { FILE *file = fopen( "release.txt", "wt" ); fputs( "release called.\n", file ); fclose( file ); // 明示的にdeleteする delete _instance; _instance = 0; } int main() { A::Instance()->Show(); A::Instance()->Release(); return 0; }