By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,282 Members | 1,562 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,282 IT Pros & Developers. It's quick & easy.

list iterator is invalid after calling cout <<

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
This is a stupid question, becasue the earse() happens in another thread
(Win32 Timer thread) rather than the allocate thread. Sorry for that.
"John" <ru*********@yahoo.com> wrote in message
news:cn**********@newton.pacific.net.sg...
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

Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.