释放属性'单例类的内存

发布于 2024-12-02 21:48:56 字数 428 浏览 1 评论 0原文

考虑到以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧梦荧光笔 2024-12-09 21:48:56

这对于调试来说确实很重要。如果您运行检测内存泄漏的工具,则由该单例引起的泄漏将污染输出,从而使检测真正的内存泄漏变得更加困难。

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.

べ繥欢鉨o。 2024-12-09 21:48:56

这取决于资源的性质。通常,最佳实践
破坏单例;破坏单例可以给
引发析构函数顺序问题。例外情况是如果单例
使用系统不会释放的资源(例如临时的
文件);在本例中,是 instance() 的“经典”实现
功能是:

Singleton& Singleton::instance()
{
    static Singleton theOneAndOnly;
    return theOneAndOnly;
}

在这种情况下,theOneAndOnly的析构函数将被调用
关闭。因此,您必须确保单例永远不会
在静态变量的析构函数中使用。

顺便说一句,你的实现也被破坏了。应该是:

Singleton& Singleton::instance()
{
    static Singleton* theOneAndOnly = new Singleton;
    return *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:

Singleton& Singleton::instance()
{
    static Singleton theOneAndOnly;
    return theOneAndOnly;
}

In this case, the destructor of theOneAndOnly will be called during
shutdown. 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:

Singleton& Singleton::instance()
{
    static Singleton* theOneAndOnly = new Singleton;
    return *theOneAndOnly;
}

This is the usual solution.

手长情犹 2024-12-09 21:48:56

正如 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.

兔姬 2024-12-09 21:48:56

如果单例实例在生命周期内保留,则无需担心释放class资源。当程序终止时,它们将消失(我不是指自动删除)。

最佳实践是在大小固定时将类对象声明为自动对象。

class SingleTon
{
//...
  BYTE pbyte1[100];
//...
};

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 automatic delete).

The best practice is to declare the class objects as automatic when the size is fixed.

class SingleTon
{
//...
  BYTE pbyte1[100];
//...
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文