2012-01-17 154 views
0

可能重复:
Does std::list::remove method call destructor of each removed element?C++ - std :: list remove_if不释放内存?

我有一个父类和子类的两个孩子Foo和Bar。类声明看起来像这样:

class Parent { 
    public: 
     virtual void action()=0; 
     std::string getName() {return name;} 
     Parent(std::string name): name(name) {} 
     virtual ~Parent() {} 
    private: 
     std::string name; 
} 
class Foo { 
    public: 
     virtual void action(); //Declared somewhere else. 
     Foo(int a, int b, unsigned long c, std::string name): Parent(name),a(a),b(b),c(c) {} 
     virtual ~Foo() {} 
    private: 
     int a,b; 
     unsigned long c; 
} 

酒吧看起来几乎与Foo相同。我不认为他们的行为职能和他们的私人成员之间的差异会产生很大的差异(这也是一堆整数)。

我需要制作一份充满Foos和酒吧的父母名单。我这样做是为了增加他们,并随后将其删除:

std::list<Parent *> pList; 
pList.push_back(new Foo(1,2,3,"Thing")); 
removeFromList(&pList, "Thing"); 

凡removeFromList定义如下:

// Binary predicate struct, find objects with matching name. 
struct NameMatch : public std::binary_function< Parent*, std::string, bool > { 
    bool operator() (Parent* p, const std::string name) const { 
     return (p->getName()==name); 
    } 
}; 

/* Removes a named object from the list of Foos. 
    Does nothing if a matching name can't be found. */ 
void removeFromList(std::list<Parent *> *pList, std::string name) { 
    pList->remove_if(std::bind2nd(NameMatch(),name)); 
} 

但是,一旦我后退出程序,Valgrind的将报告有内存泄漏,其中,由main.cpp中引用的行名单上完成的push_back操作:

==14230== 949 (520 direct, 429 indirect) bytes in 13 blocks are definitely lost in loss record 52 of 61 
==14230== at 0x4C28B35: operator new(unsigned long) (vg_replace_malloc.c:261) 
==14230== by 0x4026C8: main (main.cpp:93) 
==14230== 
==14230== 5,970 (960 direct, 5,010 indirect) bytes in 30 blocks are definitely lost in loss record 60 of 61 
==14230== at 0x4C28B35: operator new(unsigned long) (vg_replace_malloc.c:261) 
==14230== by 0x40296A: main (main.cpp:112) 

这是否意味着列表的功能的remove_if不释放内存,或者是有我在其他地方做了一个错误?我如何确保我的程序不会使用这些类泄漏内存?第二套眼睛会很有帮助。

在此先感谢! (哦,仅供参考,我无法使用Boost库进行此任务)

回答

3

您的列表包含指向对象的指针。你只是删除指针而不是释放它指向的内存(销毁对象)。在删除它之前,您需要在指针上拨打delete。这意味着list::remove_if无法在这里完成这项工作。您需要遍历列表,删除符合条件的每个对象,并使用迭代器调用list::erase

这里没有简单的出路。你需要运行时多态,所以你需要指针,你不能使用boost::shared_ptr。也许你可以作弊并使用std::shared_ptrstd::unique_ptr;)

+0

“确保我的程序不会泄漏内存”:始终使用智能指针容器而不是普通指针容器。 – ysdx 2012-01-17 00:06:02

+0

@ysdx现在他只需要智能指针。听起来像那份任务刚刚变得更加困难。 – pmr 2012-01-17 00:08:02

+0

胡男孩。感谢您的帮助! – SpeedBurner 2012-01-17 00:20:10