Re: destructor is not getting called for singleton
Note up front: You would have had an easier time figuring things out if you
had removed the template<...> first. Also, Singleton.hpp is missing and you
should have put the whole thing into a single translation unit and test
compiled it!
Anyhow...
Harry wrote:
template <class T>
T * Singleton<T>::_instance = 0;
The class Singleton holds a single T and helps creating one. It references
the singleton via a class-static pointer, right?
template <class T>
Singleton<T>::~Singleton()
{
std::cout<<"Singlton Destructor Called"<<std::endl;
delete _instance;
}
[...]
int main()
{
single::instance().test();
return 0;
}
Here, you are referencing the class Singleton<A> here, but you are not
instantiating it anywhere. Consequently, the destructor is never called as
there are no instances to destroy here.
BTW: Consider what would happen if you created two instances of this
Singleton<A>. Both would refer to the same underlying A instance, but the
first Singleton to be destroyed would also destroy that A instance. This is
probably not what you want.
Suggestion: Don't use a raw pointer but use a std::auto_ptr instead.
Uli
--
Domino Laser GmbH
Gesch??ftsf??hrer: Thorsten F??cking, Amtsgericht Hamburg HR B62 932
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]