removing an element during iteration error

Discussion in 'C++' started by brekehan, Mar 14, 2007.

  1. brekehan

    brekehan Guest

    mChannelEventQueue is a deque of pointers to ChannelEvent objects

    If the first ChannelEvent in the queue is flagged for removal. How can
    I iterate through, check for removal flag and remove without getting
    error on the 2nd iteration?


    // Remove processed events
    for(it_event = m_channelEventQueue.begin(); it_event !=
    m_channelEventQueue.end(); it_event++)
    {
    if( (*it_event)->m_processed)
    {
    delete (*it_event);
    m_channelEventQueue.erase(it_event);
    }
    }

    My compiler goves me an error that says "Assertion; Expression: ("this-
    >_Mycont !=0", 0)

    This is a built in assertion I assume. Which is coming up because
    after I erase an element, it already is at the end, and then it trys
    to increment it. Not sure how to iterate through and delete elements
    then...
     
    brekehan, Mar 14, 2007
    #1
    1. Advertising

  2. brekehan wrote:
    > mChannelEventQueue is a deque of pointers to ChannelEvent objects
    >
    > If the first ChannelEvent in the queue is flagged for removal. How can
    > I iterate through, check for removal flag and remove without getting
    > error on the 2nd iteration?


    Isn't it in the FAQ?

    >
    >
    > // Remove processed events
    > for(it_event = m_channelEventQueue.begin(); it_event !=
    > m_channelEventQueue.end(); it_event++)


    Drop the "it_event++" (BTW, you should consider using ++it_event
    instead in such situations, anyway, but do drop it).

    > {
    > if( (*it_event)->m_processed)
    > {
    > delete (*it_event);
    > m_channelEventQueue.erase(it_event);


    If your 'EventQueue' is a list, change to

    it_event = m_channelEventQueue.erace(it_event);

    > }


    Add
    else
    ++it_event;

    > }
    >
    > My compiler goves me an error that says "Assertion; Expression:
    > ("this-
    >> _Mycont !=0", 0)

    > This is a built in assertion I assume. Which is coming up because
    > after I erase an element, it already is at the end, and then it trys
    > to increment it. Not sure how to iterate through and delete elements
    > then...


    Look in the archives. There were plenty of questions on iteration
    and deletion.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 14, 2007
    #2
    1. Advertising

  3. brekehan

    Marcus Kwok Guest

    Victor Bazarov <> wrote:
    > brekehan wrote:
    >> mChannelEventQueue is a deque of pointers to ChannelEvent objects
    >>
    >> If the first ChannelEvent in the queue is flagged for removal. How can
    >> I iterate through, check for removal flag and remove without getting
    >> error on the 2nd iteration?

    >
    > Isn't it in the FAQ?


    Actually, I don't think it is, though it probably should be.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, Mar 14, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roman Suzi
    Replies:
    0
    Views:
    330
    Roman Suzi
    Jan 19, 2005
  2. Terry Reedy
    Replies:
    0
    Views:
    366
    Terry Reedy
    Jan 20, 2005
  3. robert
    Replies:
    29
    Views:
    1,051
    Raymond Hettinger
    Mar 14, 2006
  4. Jean-Paul Calderone
    Replies:
    0
    Views:
    366
    Jean-Paul Calderone
    Mar 13, 2006
  5. Rudi
    Replies:
    5
    Views:
    5,031
Loading...

Share This Page