使用单例时内存泄漏

时间:2012-05-13 14:43:02

标签: c++ memory-leaks valgrind

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

无论如何 - 我有一个内存泄漏,vlagrind将我指向这些行:

_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时会调用析构函数导致无限循环)。我如何摆脱这种泄漏?谢谢!

P.S。这是Valgrind的输出:

==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

另外:这是我创建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;
}

2 个答案:

答案 0 :(得分:4)

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

Scott Meyers在his "More Effective C++" book中对此主题进行了很好的讨论。

答案 1 :(得分:2)

使Manager成为一个静态对象,它的构造函数和析构函数将自动被调用。或者,如果必须使用operator new分配它,请将其放在智能指针中(如果可以,则使用unique_ptr,否则为auto_ptr),以便在指针出现时将其销毁。