list iterator is invalid after calling cout <<

J

John

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
 
J

John

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

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

Forum statistics

Threads
474,184
Messages
2,570,976
Members
47,536
Latest member
MistyLough

Latest Threads

Top