释放属性'单例类的内存
考虑到以下 Singleton 类,我不会在析构函数中释放内存。但这个类的实例将保留程序的生命周期,就像大多数单吨类一样。除了最佳实践感知之外,释放属性的内存真的很重要吗?
class Singleton
{
private:
Singleton() { pbyte1 = new BYTE[100]; }
~Singleton() {};
BYTE* pbyte1;
static SingletonInstance* pInstance;
public:
static Singleton* GetInstance()
{
if( pInstace ) return pInstance;
else
pInstance = new Singleton();
}
};
Considering the following Singleton class, I am not releasing memory in the destructor. But the instance of this class will remain the life-time of the program as most of the single-ton class do. Is it really matter to release the attribute's memory other than best practices perception?
class Singleton
{
private:
Singleton() { pbyte1 = new BYTE[100]; }
~Singleton() {};
BYTE* pbyte1;
static SingletonInstance* pInstance;
public:
static Singleton* GetInstance()
{
if( pInstace ) return pInstance;
else
pInstance = new Singleton();
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这对于调试来说确实很重要。如果您运行检测内存泄漏的工具,则由该单例引起的泄漏将污染输出,从而使检测真正的内存泄漏变得更加困难。
It does matter for debugging. If you run a tool for detecting memory leaks, the leaks caused by this singleton will pollute the output, making it harder to detect real memory leaks.
这取决于资源的性质。通常,最佳实践
不破坏单例;破坏单例可以给
引发析构函数顺序问题。例外情况是如果单例
使用系统不会释放的资源(例如临时的
文件);在本例中,是
instance()
的“经典”实现功能是:
在这种情况下,
theOneAndOnly
的析构函数将被调用关闭。因此,您必须确保单例永远不会
在静态变量的析构函数中使用。
顺便说一句,你的实现也被破坏了。应该是:
这是通常的解决方案。
It depends on the nature of the resources. Usually, the best practice
is not to destruct the singleton; destructing the singleton can give
rise to order of destructor problems. The exception is if the singleton
uses resources which won't be freed by the system (e.g. temporary
files); in this case, the "classical" implementation of the
instance()
function is:
In this case, the destructor of
theOneAndOnly
will be called duringshutdown. For this reason, you must ensure that the singleton is never
used in the destructor of a static variable.
By the way, you're implementation is broken as well. It should be:
This is the usual solution.
正如 Björn 所说:这对于调试很重要。
这对于维护也很重要。程序将会改变。它将被重构。如果在重构过程中,单例突然不再是单例,则丢失的析构函数可能会引入真正的泄漏。此外,将来该对象可能需要的不仅仅是内存(例如数据库句柄)。关闭程序时不返回它们是一个真正的错误。
因此,如果您想要最佳实践:每个析构函数都应返回对象拥有的资源。这包括您可以依赖操作系统在程序关闭时获取它们的对象的析构函数。
As Björn said: it matters for debugging.
It matters also for maintenance. The program will change. It will be refactored. If during refactoring a singleton suddenly isn't a singleton anymore, the missing destructor might introduce real leaks. Additionally, in the future the object might need more than memory (e.g. db handles). Not returning them on closing the program is a true error.
So if you want a best practice: Every destructor should return the resources the object owns. This includes destructors of objects where you could rely on the OS to take them on program shutdown.
如果单例实例在生命周期内保留,则无需担心释放
class
资源。当程序终止时,它们将消失(我不是指自动删除
)。最佳实践是在大小固定时将类对象声明为自动对象。
If the singleton instance is remaining for the lifetime, then no need to worry about releasing the
class
resources. When the program terminates, they will vanish (I don't mean automaticdelete
).The best practice is to declare the class objects as automatic when the size is fixed.