Should Singleton instance be destructed after its usage
The following question is NOT a homework problem. I am asking it for
learning purpose only.
Suppose I have a Singleton class and I have created an instance of the
Singleton class. Now, after the usage of the Singleton instance,
should the instance be deleted ? If it should be deleted, which of the
following two versions of deleting the Singleton instance should be
preferred ?
Program version 1:
---------------------------
#include <cstdlib>
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
template <typename T> class SingletonDestroyer
{
public:
SingletonDestroyer();
~SingletonDestroyer();
};
template <typename T> inline
SingletonDestroyer<T>::SingletonDestroyer()
{
}
template <typename T> inline
SingletonDestroyer<T>::~SingletonDestroyer()
{
cout << "SingletonDestroyer dtor called" << endl;
delete T::p;
T::p = 0;
}
class Singleton
{
public:
friend SingletonDestroyer<Singleton>::~SingletonDestroyer();
static Singleton* getInstance(void);
int value(void) const;
void value(int arg);
private:
Singleton();
Singleton(const Singleton& arg);
~Singleton();
static bool instanceCreated;
static Singleton* p;
int val;
};
bool Singleton::instanceCreated = false;
Singleton* Singleton::p = 0;
inline Singleton::Singleton()
{
}
Singleton* Singleton::getInstance(void)
{
if (p)
return p;
if (instanceCreated)
{
const string e("Singleton instance already created
"
"but later destroyed");
cout << "From Singleton::getInstance(): " << e
<< endl;
throw logic_error(e);
}
p = new Singleton();
instanceCreated = true;
return p;
}
inline int Singleton::value() const
{
return val;
}
inline void Singleton::value(int arg)
{
val = arg;
return;
}
inline Singleton::~Singleton()
{
cout << "Singleton dtor called" << endl;
}
int main()
{
// first block
{
SingletonDestroyer<Singleton> sd;
Singleton::getInstance()->value(100);
cout << Singleton::getInstance()->value() << endl;
}
try
{
SingletonDestroyer<Singleton> sd;
Singleton::getInstance()->value(200);
cout << Singleton::getInstance()->value() << endl;
}
catch (const logic_error& e)
{
cout << "logic_error exception thrown: " << e.what()
<< endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Program version 2:
----------------------------
#include <cstdlib>
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
class Singleton
{
public:
friend class SingletonDestroyer;
static Singleton* getInstance(void);
int value(void) const;
void value(int arg);
private:
Singleton();
Singleton(const Singleton& arg);
~Singleton();
static bool instanceCreated;
static Singleton* p;
int val;
};
bool Singleton::instanceCreated = false;
Singleton* Singleton::p = 0;
inline Singleton::Singleton()
{
}
Singleton* Singleton::getInstance(void)
{
if (p)
return p;
if (instanceCreated)
{
const string e("Singleton instance already created "
"but later destroyed");
cout << "From Singleton::getInstance(): " << e
<< endl;
throw logic_error(e);
}
p = new Singleton();
instanceCreated = true;
return p;
}
inline int Singleton::value() const
{
return val;
}
inline void Singleton::value(int arg)
{
val = arg;
return;
}
inline Singleton::~Singleton()
{
cout << "Singleton dtor called" << endl;
}
class SingletonDestroyer
{
public:
SingletonDestroyer();
~SingletonDestroyer();
};
inline SingletonDestroyer::SingletonDestroyer()
{
}
inline SingletonDestroyer::~SingletonDestroyer()
{
cout << "SingletonDestroyer dtor called" << endl;
delete Singleton::p;
Singleton::p = 0;
}
int main()
{
// first block
{
SingletonDestroyer sd;
Singleton::getInstance()->value(100);
cout << Singleton::getInstance()->value() << endl;
}
try
{
SingletonDestroyer sd;
Singleton::getInstance()->value(200);
cout << Singleton::getInstance()->value() << endl;
}
catch (const logic_error& e)
{
cout << "logic_error exception thrown: " << e.what()
<< endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Kindly explain.
Thanks
V.Subramanian