Hello,
got a C++ doubt..
normally Singleton class is written like
Sing.h
==========
class Sing{
private:
Sing();
static Sing* instance;
public:
static Sing *getInstance;
};
Sing.cpp
========
Code:
#include "Sing.h"
Sing::Sing()
{
}
Sing* Sing::instance = 0;
Sing* Sing::getInstance()
{
if (instance == 0)
{
instance = new Sing();
}
return instance;
}
In case of singleton, dest never gets called. So, mem allocated using new does not have corresponding delete. So, this can lead to mem leak.
Hence, i heard this can be solved using
Sing* Sing::getInstance()
{
static Sing instance;
return &instance;
}
But this proc can be used only for single thread. not for multi.
Why so. Why cant it be used for multithreading??
-Regards,
molus