Singleton:有内存泄漏吗? [英] Singleton: is there a memory leak?

查看:109
本文介绍了Singleton:有内存泄漏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的单例:

  class Singleton 
{
Singleton();
virtual〜Singleton();

Singleton * Singleton :: getInstance()
{
static Singleton * instance;

if(!instance){
instance = new Singleton();
};
return instance;
};
}

当主代码调用 Singleton :: getInstance - > someMethod()第一次,不是实例化两次的类?会出现内存泄漏吗?



我问,因为 Visual Leak Detector 检测到 ()

解决方案


当主代码调用 Singleton :: getInstance() - > someMethod()第一次不是类实例化两次?


否。调用 Singleton 的静态成员不会实例化 Singleton ,因此 Singleton 存在这里是你创建的 new 。你只创建一次,因为一旦实例指向它,你再也不会调用 new



但是,这里有一个问题是你未能使 getInstance 静态成员函数。我认为这是一个错字/疏忽,否则,你的程序甚至不会编译。事实上,即使作为非静态成员,声明也是错误的。此外,构造函数应该是 private 以强制执行只有 getInstance 可以实例化类型的概念。


会有内存泄漏吗?


是什么泄漏检测器报告。但是,它是最小的:问题是,在你的程序关闭之前没有任何 delete 单例实例。



<坦率地说,我不会担心它。这可能是一个罕见的泄漏是可以接受的之一,主要是因为泄漏,它只是一次性失败在过程关闭时取消分配。



但是,如果你完全摆脱指针,那么你可以同时避免这两个问题,因为你的程序所做的最后一件事情将是破坏静态存储持续时间的对象: / p>

  #include< iostream> 

class Singleton
{
public:
〜Singleton(){std :: cout< destroy!\\\
; }

static Singleton& getInstance()
{
static Singleton实例;
return instance;
}

void foo(){std :: cout< foo!\\\
; }

private:
Singleton(){std :: cout< construction!\\\
; }
};

int main()
{
Singleton :: getInstance()。foo();
}

//输出:
//构建!
// foo!
//销毁!



现场演示



甚至不需要指针!



有利于整个函数本质上是线程安全的,至少是C ++ 11。


This is a simple singleton:

class Singleton
{
    Singleton();
    virtual ~Singleton();

    Singleton * Singleton::getInstance() 
    {
        static Singleton * instance;

        if (!instance) {
            instance = new Singleton();
        };
        return instance;
    };
}

When main code calls Singleton::getInstance()->someMethod() for the first time, isn't the class instantiated twice? Will be there memory leak?

I am asking because Visual Leak Detector detects memory leak on the line with new Singleton().

解决方案

When main code calls Singleton::getInstance()->someMethod() for the first time, isn't the class instantiated twice?

No. Calling a static member of Singleton does not instantiate Singleton, so the only instance of Singleton to exist here is the one you create with new. And you only create it once, because once instance points to it, you never call new again.

One problem you have here, however, is that you failed to make getInstance a static member function. I assume this is a typo/oversight since, otherwise, your program would not even compile. In fact, the declaration is ill-formed even as a non-static member. Furthermore, the constructor should be private to enforce the notion that only getInstance may instantiate the type.

Will be there memory leak?

Yes, and this is what Leak Detector is reporting. However, it's minimal: the problem is that there is nothing to delete that singleton instance right before your program shuts down.

Frankly I wouldn't worry about it. This may be one of those rare times that a leak is acceptable, mostly because more than a "leak" it's just a one-time failure to de-allocate on process shutdown.

However, if you get rid of the pointer entirely then you can avoid both problems at the same time, since one of the last things your program does will be to destroy objects of static storage duration:

#include <iostream>

class Singleton
{
public:
    ~Singleton()  { std::cout << "destruction!\n"; }

    static Singleton& getInstance() 
    {
        static Singleton instance;
        return instance;
    }

    void foo() { std::cout << "foo!\n"; }

private:
    Singleton() { std::cout << "construction!\n"; }
};

int main()
{
    Singleton::getInstance().foo();
}

// Output:
//   construction!
//   foo!
//   destruction!

(live demo)

No need even for a pointer!

This has the added benefit that the entire function becomes inherently thread-safe, at least as of C++11.

这篇关于Singleton:有内存泄漏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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