title: C++ - std::list remove_if doesn't deallocate memory?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/4260464/does-stdlistremove-method-call-destructor-of-each-removed-element">Does std::list::remove method call destructor of each removed element?</a>  </p>
  </blockquote>
  
  
  
  <p>I have a Parent class and subclassed the two children Foo and Bar. Class declarations look something like this:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>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;
  }
  </code></pre>
  
  <p>Bar looks pretty much the same as Foo. I don't think the differences in their action functions and their private members will make much of a difference (it's also a bunch of ints).</p>
  
  <p>I need to make a list of Parents filled with Foos and Bars. I do this to add them in and subsequently remove them:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>std::list&lt;Parent *&gt; pList;
  pList.push_back(new Foo(1,2,3,"Thing"));
  removeFromList(&amp;pList, "Thing");
  </code></pre>
  
  <p>Where removeFromList is defined as follows:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>// Binary predicate struct, find objects with matching name.
  struct NameMatch : public std::binary_function&lt; Parent*, std::string, bool &gt; {
       bool operator () (Parent* p, const std::string name) const {
           return (p-&gt;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&lt;Parent *&gt; *pList, std::string name) {
      pList-&gt;remove_if(std::bind2nd(NameMatch(),name));
  }
  </code></pre>
  
  <p>However, once I exit the program after, Valgrind will report there are memory leaks, where the lines referenced by main.cpp are the push_back operations done on the list:</p>
  
  <pre><code>==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)
  </code></pre>
  
  <p>Does this mean that the list's remove_if function doesn't deallocate memory, or is there an error I made somewhere else? And how would I make sure that my program won't leak memory from using these classes? A second set of eyes would be really helpful.</p>
  
  <p>Thanks in advance! (Oh, just an FYI, I can't use the Boost libraries for this assignment)</p>