Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7064101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:47:34+00:00 2026-05-28T04:47:34+00:00

Possible Duplicate: Does std::list::remove method call destructor of each removed element? I have a

  • 0

Possible Duplicate:
Does std::list::remove method call destructor of each removed element?

I have a Parent class and subclassed the two children Foo and Bar. Class declarations look something like this:

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;
}

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).

I need to make a list of Parents filled with Foos and Bars. I do this to add them in and subsequently remove them:

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

Where removeFromList is defined as follows:

// 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));
}

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:

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

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.

Thanks in advance! (Oh, just an FYI, I can’t use the Boost libraries for this assignment)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:47:35+00:00Added an answer on May 28, 2026 at 4:47 am

    Your list contain pointers to objects. You are only removing the pointer but not deallocating the memory it points to (destroying the object). You need to call delete on the pointer before removing it. This means that a list::remove_if cannot do the job here. You need to traverse the list, delete each object that matches the criteria and call list::erase with the iterator.

    There is no easy way out here. You need runtime-polymorphism, so you need pointers and you cannot use boost::shared_ptr. Maybe you can cheat and use std::shared_ptr or std::unique_ptr 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Does std::list::remove method call destructor of each removed element? I have a
Possible Duplicate: Is std::list<>::sort stable? Does C++ std::list sort function is guaranteed to preserve
Possible Duplicate: What is The Rule of Three? How exactly does std::pair call destructors
Possible Duplicate: Accessing inherited variable from templated parent class I have been implementing a
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: Are std::vector elements guaranteed to be contiguous? does std::vector always contain the
Possible Duplicates: C++ method only visible when object cast to base class?! Why does
Possible Duplicate: Does an abstact classes have a VTABLE? Does a vtable gets created
Possible Duplicate: Does SQL Server 2005 have an equivalent to MySql’s ENUM data type?
Possible Duplicate: What does 'unsigned temp:3' means I have been trying to learn raw

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.