Thread: STL list - Assertion Error :(

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Question STL list - Assertion Error :(

    Hi ppl,

    I am not very good with STL and I am getting an error in trying to delete the contents of an iterator and the iterator itself and then continue without them.
    I am not sure how to overcome the error.

    Any help would be greatly appreciated as to how this can be overcome.

    Code:
      RECT          rcLastSpPos;
      SPRITEACTION  saSpriteAction;
      list<Sprite*>::iterator SpriteIterator;
      for (SpriteIterator = m_lSprites.begin(); SpriteIterator != m_lSprites.end(); SpriteIterator++)
      {
        rcLastSpPos = (*SpriteIterator)->GetPosition();
    
        // Update the sprite
        saSpriteAction = (*SpriteIterator)->Update();
    
        // Handle the SA_KILL sprite action
        if (saSpriteAction & SA_KILL)
        {
          // Destroy the sprite
          delete (*SpriteIterator);
          m_lSprites.erase(SpriteIterator);
          SpriteIterator--;
          continue;
        }
    etc....etc...
        
      }
    List iterator not incremental.... Assertion Error



    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This sounds familiar, but anyway:
    Code:
    RECT          rcLastSpPos;
    SPRITEACTION  saSpriteAction;
    list<Sprite*>::iterator SpriteIterator = m_lSprites.begin();
    while (SpriteIterator != m_lSprites.end())
    {
        rcLastSpPos = (*SpriteIterator)->GetPosition();
    
        // Update the sprite
        saSpriteAction = (*SpriteIterator)->Update();
    
        // Handle the SA_KILL sprite action
        if (saSpriteAction & SA_KILL)
        {
            // Destroy the sprite
            delete *SpriteIterator;
            m_lSprites.erase(SpriteIterator++);
        }
        else
        {
            // etc....etc...
            ++SpriteIterator;
        }
    }
    The important part is about avoiding iterator invalidation by using post-increment.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by laserlight View Post
    The important part is about avoiding iterator invalidation by using post-increment.
    Yes it does sound familiar, and again I don't understand why you're teaching people to use erase that way. It's very non-intuitive, and I doubt most people would understand why post-increment works and pre-increment doesn't. Then if someone else sees this person's code, they might not know that pre-inc wont also work.
    I always recommend this:
    Code:
           SpriteIterator = m_lSprites.erase(SpriteIterator);
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM