Thread: iterator problem

  1. #1
    Unregistered
    Guest

    Question iterator problem

    Hi all,

    I have a list of pointers to objects and I am trying to delete the objects before clearing the list. Here's the code:

    .................................................. .............................................

    1: for (iter = addrlist.begin(); iter != addrlist.end(); iter++)
    2: {
    3: delete iter;
    4: }
    5:
    6: this->addrlist.clear();

    .................................................. ...............................................

    I tried de-referencing iter but it kept crapping out on line 3. In line 3, iter points to a cell in the list that holds a pointer to an object. I want to delete the object first, how do I do that? TIA


    BTW, here's how I am populating the list:
    .................................................. ................................................

    1: Addresse* add = new Addresse();
    2: add->setID(LastRecordId++);
    3: this->addrlist.push_back(*add);

    .................................................. ................................................

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    As you have a list of pointers to Addresse objects, then the iterator will be a pointer to a pointer to an Addresse object. So de-referencing iter before deleting should work.

    Are you doing any memory management in your Addresse destructor?

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Code:
     template <class container> void freeDynamicContainer(container& c) {
      for (typename container::iterator i = c.begin(); i != c.end(); i++) {
       delete (*i);
      }
      c.clear();
     }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    Unregistered
    Guest
    Originally posted by Sorensen

    Are you doing any memory management in your Addresse destructor?
    Yes, I am trying to free memory in my destructor before exiting program.

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Then if you were de-referencing iter, as per SilentStrikes example, and get the crash then it's likely that the error is in your destructor not your list deleting code.

  6. #6
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    ...To add on to Sorensen's post,

    you may be deleting the data twice.

    p.s.

    When the std::list goes out of scope, can you overload it's destructor (if it has one ??) so that it does the memory management ?

  7. #7
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    std::list (or any of the standard containers) doesn't have a virtual destructor... so you can't really do it. I was suprised g++ even let me compile this without warning (despite deriving from class with non-virtual destructor)

    Code:
    #include <list>
    #include <iostream>
    
    class check {
    public:
     check() { count++;}
     ~check() {count--;}
     static int getCount() { return count; }
    private:
     static int count;
    };
    
    int check::count=0;
    
    template <class T> class ptrList: public std::list<T*> {
    public:
     ptrList() { }
     ~ptrList() {
      for (std::list<T*>::iterator i = begin(); i != end(); i++) {
       delete *i;
      }
     }
    };
    
    int main() {
     std::list<check*>* blah = new ptrList<check>;
     blah->push_back(new check);
     blah->push_back(new check);
     delete blah;  // doesn't call ~ptrList() because of non-vrituality of ~std::list()
     std::cout << check::getCount() << std::endl; // would be 0 if they were deleted..
     return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed