静态字段的析构函数。单例实现 [英] Destructor for static fields. Singleton realization

查看:187
本文介绍了静态字段的析构函数。单例实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,经典的简单的Singleton实现如下:

So, classic simple Singleton realization is following:

class Singleton
{
private:
    static Singleton* singleton;
    Singleton() {}
public:
    static Singleton* getInstance();        
};

cpp-file

Singleton* Singleton::singleton = 0;

Singleton* Singleton::getInstance()
{
    if (!singleton)
    {
        singleton = new Singleton;
    }

    return singleton;
}

我在这里看到内存泄漏 - 'cos没有删除新的。但是在C ++中没有静态析构函数,所以我们只是不关心这个内存泄漏?

I see memory leak here - 'cos there is no delete for the new. But in C++ there isn't static destructor, so we just don't care about this memory leak?

推荐答案

内存泄漏不仅仅是一个没有匹配的配置。这是当你有可以回收的内存,因为对象不再被使用,但是实际上没有被释放。事实上,许多内存泄漏是程序中有代码来释放内存的情况,但无论什么原因,它都不会被调用(例如参考循环)。事实上,关于如何检测这些泄漏有很多研究; 本文 是一个这样的工具的一个很好的例子。

A memory leak is more than just an allocation with no matching free. It's when you have memory that could be reclaimed because the object is no longer in use, but which doesn't ever actually get freed. In fact, many memory leaks are cases where there is code in the program to deallocate memory, but for whatever reason it doesn't get called (for example, a reference cycle). In fact, there's a lot of research on how to detect these sorts of leaks; this paper is an excellent example of one such tool.

在单例的情况下,我们没有漏洞,因为整个程序中存在单例。它的生命永远不会结束,所以没有回收的记忆不是一个问题。

In the case of a singleton, we don't have a leak because that singleton exists throughout the program. Its lifetime is never intended to end, and so the memory not getting reclaimed isn't a problem.

那就是说,你上面的代码不是大多数人会实行单身人士规范的C ++实现将是这样的:

That said, the code you have above is not how most people would implement a singleton. The canonical C++ implementation would be something like this:

class Singleton
{
private:
    /* No instantiation. */
    Singleton() {}

    /* Explicitly disallow copying. */ 
    Singleton(const Singleton&) = delete;
    Singleton& operator= (const Singleton&) = delete;

    /* In C++03, the above would be written as
     *
     *    Singleton(const Singleton&);
     *    Singleton& operator= (const Singleton&);
     * 
     * and you'd just leave the methods unimplemented.
     */
public:
    static Singleton& getInstance();        
};

.cpp文件:

Singleton& Singleton::getInstance() {
    /* Have a static local variable representing the unique instance.  Since
     * it's static, there is only one instance of this variable.  It's also only
     * initialized when getInstance is called.
     */
    static Singleton theInstance;
    return theInstance;
}

现在根本没有动态分配 - 内存由编译器分配,可能驻留在代码或数据段而不是堆中。还要注意,你必须明确禁止复制,否则你可能会得到许多单身人士的克隆。

Now there's no dynamic allocation at all - the memory is allocated by the compiler and probably resides in the code or data segment rather than in the heap. Also note that you have to explicitly disallow copying, or otherwise you could end up with many clones of the singleton.

另一个优点是C ++保证在程序中退出(假设程序正常终止), theInstance 的析构函数将确实在程序结束时触发。您可以使用所需的所有清理代码定义析构函数。

The other advantage of this is that C++ guarantees that at program exit (assuming the program terminates normally), the destructor for theInstance will indeed fire at the end of the program. You can thus define a destructor with all the cleanup code you need.

希望这有帮助!

这篇关于静态字段的析构函数。单例实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆