Singleton Implementation的任何内存泄漏 [英] Any memory leak for this Singleton Implementation
问题描述
我多次见过,
//周四:
等级T
{
public:
static T * instance();
private:
T(){}
~T(){}
静态T * smInstance;
};
// T.cpp :
T * T :: smInstance = NULL;
T * T :: instance()
{
if(smInstance == NULL)
smInstance = new T();
返回smInstance;
}
但我怀疑有内存泄漏,因为有一个free()调用
为new()分配的内存。
然而,我多次看到我倾向于怀疑自己 - 一个类似的
实现甚至出现在c ++ cook book上,这是一本非常新的书。
O''really。
任何人都可以帮忙澄清一下吗?谢谢。
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.
推荐答案
tomgee写道:
tomgee wrote:
我看了很多时间,
// Th:
上课T
{
公开:
静态T * instance();
私人:
T(){}
~T(){}
静态T * smInstance;
};
// T.cpp:
T * T :: smInstance = NULL;
T * T :: instance()
{
if(smInstance == NULL)
smInstance = new T();
返回smInstance;
}
但我怀疑有内存泄漏,因为有一个free()对于由new()分配的内存调用
。
然而,我多次看到我倾向于怀疑自己 - 类似的实现甚至出现在c ++上烹饪书,这是一本非常新的书,作者是O''really。
任何人都可以帮忙澄清一下?谢谢。
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.
我认为你的意思更像是:
模板< class T>
class Singleton
{
// ...
};
它''如果您的程序永远不会停止运行或者操作系统
在您之后清理,那么这不是内存泄漏,其中一个可能在所有现代的b $ b系统中都是正确的。查看_Modern C ++ Design_的第6章,比以往任何时候都要多。
想知道如何用C ++实现单例。
干杯! --M
I think you mean something more like:
template<class T>
class Singleton
{
// ...
};
It''s not a memory leak if your program never stops running or if the OS
cleans up after you, one of which is probably true on all modern
systems. See chapter 6 of _Modern C++ Design_ for more than you ever
wanted to know about implementing singletons in C++.
Cheers! --M
不,它不是模板化的。我很抱歉使用了这个误导性的课程
名称。
如何确保操作系统能够清理它,而无需调用
免费()?你能解释一下这里的一些实现吗,我只是关于动态内存分配和声明的问题吗?
谢谢。
>
No, it''s not templated. I am sorry to have used this misleading class
name.
How can you make sure the OS will clean it up, without calling a
free()? Can you please explain a little implementation here, I am only
concerned about the dynamic memory allocation and claiming?
Thanks.
tomgee写道:
我看过很多次了,
//上课:
T
{
公开:
静态T *实例();
私人:
T(){}
~T(){}
static T * smInstance;
};
// T.cpp:
T * T :: smInstance = NULL;
T * T :: instance()
{
if(smInstance == NULL)
smInstance = new T();
返回smInstance;
}
但我怀疑存在内存泄漏,因为对new()分配的内存有一个free()调用
。
我怀疑你的意思是*没有*免费电话。
你*应该*说的是没有电话删除。
然而,我多次看到我倾向于怀疑自己 - 一个类似的实现甚至出现在c ++ cook book上,这是一本非常新的书。奥利尔。
任何人都可以帮忙澄清一下吗?谢谢。
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().
I suspect you meant there is *NO* call to free.
What you *should* have said is that there is no call to delete.
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.
什么时候smInstance解构了?
Ben Pope
-
我不只是一个数字。对很多人来说,我被称为字符串...
When is smInstance deconstructed?
Ben Pope
--
I''m not just a number. To many, I''m known as a string...
这篇关于Singleton Implementation的任何内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!