STL list - Assertion Error :(

This is a discussion on STL list - Assertion Error :( within the Windows Programming forums, part of the Platform Specific Boards category; Hi ppl, I am not very good with STL and I am getting an error in trying to delete the ...

  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
    21,598
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,299
    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, 03: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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21