list iterator is invalid after calling cout <<

Discussion in 'C++' started by John, Nov 23, 2004.

  1. John

    John Guest

    Hi, all

    This is a question about the iterator on list

    In this code below, I check the time out for all the connected socket in a
    list. The list keep the point of each connected socket object, i.e,
    list<CChatSocket*> In code CheckConnections(), if the connection time out,
    it call process_time_out.

    The problem I am facing is,
    The program assert on line B, if line C is not comment out. Is shows that
    iter is not valid anymore after calling process_time_out(). However, when I
    comment out line C, the program runs OK.

    Dose anyone can tell why cout has effect on the iterator? Or I just totally
    missed something.


    static void CChatSocket::CheckConnections(
    PVOID lpParameter,
    BOOLEAN TimerOrWaitFired
    )
    {
    ASSERT(lpParameter == NULL);
    ASSERT(TimerOrWaitFired);
    list<CChatSocket*>::iterator iter;

    for (iter = m_socketList.begin(); iter != m_socketList.end(); )
    {
    CChatSocket* pSocket = *iter;

    if (is_time_out(pSocket))
    {
    process_time_out(pSocket); // *** line A
    iter = m_socketList.erase(iter); // *** line B
    }
    else
    {
    ++ iter;
    }
    }
    }

    static void CChatSocket::process_time_out(CChatSocket* pSocket)
    {
    pSocket->Shutdown();
    pSocket->Close();
    pSocket->Release();
    cout << "socket disconnected becasue of timeout" << endl; // *** line C
    }



    Thank,

    John
    John, Nov 23, 2004
    #1
    1. Advertising

  2. John

    John Guest

    Re: list iterator is invalid after calling cout << - please ignor

    This is a stupid question, becasue the earse() happens in another thread
    (Win32 Timer thread) rather than the allocate thread. Sorry for that.


    "John" <> wrote in message
    news:cnuep2$jnm$...
    > Hi, all
    >
    > This is a question about the iterator on list
    >
    > In this code below, I check the time out for all the connected socket in a
    > list. The list keep the point of each connected socket object, i.e,
    > list<CChatSocket*> In code CheckConnections(), if the connection time out,
    > it call process_time_out.
    >
    > The problem I am facing is,
    > The program assert on line B, if line C is not comment out. Is shows that
    > iter is not valid anymore after calling process_time_out(). However, when
    > I comment out line C, the program runs OK.
    >
    > Dose anyone can tell why cout has effect on the iterator? Or I just
    > totally missed something.
    >
    >
    > static void CChatSocket::CheckConnections(
    > PVOID lpParameter,
    > BOOLEAN TimerOrWaitFired
    > )
    > {
    > ASSERT(lpParameter == NULL);
    > ASSERT(TimerOrWaitFired);
    > list<CChatSocket*>::iterator iter;
    >
    > for (iter = m_socketList.begin(); iter != m_socketList.end(); )
    > {
    > CChatSocket* pSocket = *iter;
    >
    > if (is_time_out(pSocket))
    > {
    > process_time_out(pSocket); // *** line A
    > iter = m_socketList.erase(iter); // *** line B
    > }
    > else
    > {
    > ++ iter;
    > }
    > }
    > }
    >
    > static void CChatSocket::process_time_out(CChatSocket* pSocket)
    > {
    > pSocket->Shutdown();
    > pSocket->Close();
    > pSocket->Release();
    > cout << "socket disconnected becasue of timeout" << endl; // *** line
    > C
    > }
    >
    >
    >
    > Thank,
    >
    > John
    >
    >
    John, Nov 23, 2004
    #2
    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. abi

    man cout or info cout

    abi, Jun 27, 2003, in forum: C++
    Replies:
    2
    Views:
    1,633
  2. Pmb

    std::cout vs cout

    Pmb, Jun 2, 2004, in forum: C++
    Replies:
    2
    Views:
    4,389
    Leor Zolman
    Jun 2, 2004
  3. Replies:
    4
    Views:
    307
    Howard
    Sep 15, 2005
  4. Replies:
    6
    Views:
    627
    Jim Langston
    Oct 30, 2005
  5. Stefan Ram

    Re: cout vs std::cout

    Stefan Ram, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    459
Loading...

Share This Page