T
tomgee
I saw it many times,
// T.h:
class T
{
public:
static T* instance();
private:
T() {}
~T() {}
static T* smInstance;
};
// T.cpp:
T* T::smInstance = NULL;
T* T::instance()
{
if (smInstance == NULL)
smInstance = new T();
return smInstance;
}
But I suspected there is a memory leak, because there is a free() call
for the memory allocated by new().
However, I saw it many times that I tend to doubt myself - a similiar
implementation even appears on the "c++ cook book", a very new book by
O'really.
Anybody can help to clarify? Thanks.
// T.h:
class T
{
public:
static T* instance();
private:
T() {}
~T() {}
static T* smInstance;
};
// T.cpp:
T* T::smInstance = NULL;
T* T::instance()
{
if (smInstance == NULL)
smInstance = new T();
return smInstance;
}
But I suspected there is a memory leak, because there is a free() call
for the memory allocated by new().
However, I saw it many times that I tend to doubt myself - a similiar
implementation even appears on the "c++ cook book", a very new book by
O'really.
Anybody can help to clarify? Thanks.