for_each iteration in a std::list adds a new element and nextiteration crashes

Discussion in 'C++' started by Javier, Apr 18, 2008.

  1. Javier

    Javier Guest

    Hi all,

    I have this code:

    class A
    {
    std::list<B> m_observadores;

    void function()
    {

    ApplySendMessage apply(this, message);
    std::for_each(m_observadores.begin(),
    m_observadores.end(),
    apply);
    }
    }

    and, ApplySendMessage::eek:perator() adds a new item to the list
    m_observadores under some conditions.

    I get (MSVC8.0) "list iterator not incrementable" in an iteration of
    for_each algorithm after adding (m_observadores.push_back) a new item
    to the list in the previous iteration (ApplySendMessage =
    AplicarEnviarMensaje):

    msvcp80d.dll!std::_Debug_message(const wchar_t * message=0x005fbf30,
    const wchar_t * file=0x005fbb50, unsigned int line=236) Línea 24 C++
    > libcliente.dll!std::list<maevif::cliente::ObservadorGestorEntidades *,std::allocator<maevif::cliente::ObservadorGestorEntidades *> >::_Const_iterator<0>::eek:perator++() Línea 236 + 0x17 bytes C++

    libcliente.dll!std::list<maevif::cliente::ObservadorGestorEntidades
    *,std::allocator<maevif::cliente::ObservadorGestorEntidades *>
    >::_Iterator<0>::eek:perator++() Línea 399 C++

    libcliente.dll!
    std::for_each<std::list<maevif::cliente::ObservadorGestorEntidades
    *,std::allocator<maevif::cliente::ObservadorGestorEntidades *>
    >::_Iterator<1>,maevif::cliente::AplicarEnviarMensaje>(std::list<maevif::cliente::ObservadorGestorEntidades

    *,std::allocator<maevif::cliente::ObservadorGestorEntidades *>
    >::_Iterator<1> _First=0x0249b93c,

    std::list<maevif::cliente::ObservadorGestorEntidades
    *,std::allocator<maevif::cliente::ObservadorGestorEntidades *>
    >::_Iterator<1> _Last=0xcdcdcdcd,

    maevif::cliente::AplicarEnviarMensaje _Func={...}) Línea 28 + 0x8
    bytes C++


    Is the iterator invalidated after the insertion or my error comes from
    another place? Is this standard?

    m_observadores.push_front instead of m_observadores.push_back will fix
    that?

    Thank you.
     
    Javier, Apr 18, 2008
    #1
    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. Dylan

    std::for_each question

    Dylan, Jul 4, 2004, in forum: C++
    Replies:
    5
    Views:
    8,561
    =?iso-8859-1?q?Josep_Mon=E9s_i_Teixidor?=
    Jul 5, 2004
  2. Philip Potter

    std::for_each

    Philip Potter, Aug 29, 2006, in forum: C++
    Replies:
    6
    Views:
    684
    Shooting
    Aug 29, 2006
  3. Replies:
    2
    Views:
    319
    David Harmon
    Mar 14, 2007
  4. Christopher
    Replies:
    5
    Views:
    623
    James Kanze
    Dec 20, 2007
  5. Rudi
    Replies:
    5
    Views:
    5,023
Loading...

Share This Page