使用单例时发生内存泄漏 [英] Memory leaks when using a singleton

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

问题描述

我有一个实现singelton设计模式的类. 我知道有些人不认为这是个好主意,但这很有帮助,

I have a class in which I implement the singelton design pattern. I know some people don't think its a good idea, but it helps a lot,

无论如何-我发生了内存泄漏,vlagrind将我指向了以下几行:

Anyway - I have a memory leak and vlagrind points me to these lines:

_singleton = new Manager(); //Manager::instance() (Manager.cpp:18)

还有

Manager::Manager() : _file(new ofstream), _tasks(new map<int, Task *>()),
        _idState(new map<int, int>()), _closing(false), _pending(false),
        _lock(new pthread_mutex_t), _endLock(new pthread_mutex_t),  _cond(new pthread_cond_t),
        _flushCond(new map<int, pthread_cond_t *>()), _attr(new pthread_attr_t) {
//The last line is line 25 in Manager::Manager

现在在Manager的析构函数中,我无法显式删除它,因为它会创建一个愚蠢的循环(因为删除_singleton会导致无限循环时将调用析构函数).我如何摆脱这种泄漏?谢谢!

Now in Manager's destructor I can't explicitly delete it, because it creates a silly loop (as destructor will be called when deleting _singleton resulting in an infinite loop). How do I get rid of this leak? Thanks!

P.s.这是Valgrind的输出:

P.s. here is Valgrind's output:

==17823== 512 bytes in 1 blocks are definitely lost in loss record 2 of 2
==17823==    at 0x4C27297: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==17823==    by 0x40151E: Manager::Manager() (Manager.cpp:25)
==17823==    by 0x4014DB: Manager::instance() (Manager.cpp:18)
==17823==    by 0x406475: initdevice(char*) (outputdevice.cpp:66)
==17823==    by 0x4061D5: main (driver.cpp:21)
==17823== 
==17823== LEAK SUMMARY:
==17823==    definitely lost: 512 bytes in 1 blocks
=    =17823==    indirectly lost: 0 bytes in 0 blocks
==17823==      possibly lost: 288 bytes in 1 blocks
==17823==    still reachable: 0 bytes in 0 blocks
==17823==         suppressed: 0 bytes in 0 blocks

添加:这是我创建管理器的代码:

Addition: here's the code where I create Manager:

Manager.h:
class Manager {
public:
    ~Manager();
    static Manager * instance();
private:
    Manager();
    static Manager * _singleton;
};

执行:

Manager.cpp:
Manager * Manager::_singleton = 0;

Manager * Manager::instance() {
    if (!_singleton)
        _singleton = new Manager();
    return _singleton;
}

推荐答案

在C ++中实现单例的一种常见方法是使实例成为实例getter内的函数静态std::unique_ptr<T>,而不是类静态变量.这样可以确保在程序完成时调用析构函数,并让您创建可以多态访问的实例,例如通过指向抽象基类的指针.

One common way of implementing singleton in C++ is making the instance a function-static std::unique_ptr<T> inside the instance getter, rather than a class-static variable. This ensures a call of destructor upon program's completion, and lets you create an instance that gets accessed polymorphically e.g. through a pointer to an abstract base class.

Scott Meyers在他的更有效的C ++"书中对此主题进行了很好的讨论.

Scott Meyers provided a good discussion of this topic in his "More Effective C++" book.

这篇关于使用单例时发生内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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