removing an element during iteration error

B

brekehan

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

Victor Bazarov

brekehan said:
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-
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top