Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > list iterator is invalid after calling cout <<

Reply
Thread Tools

list iterator is invalid after calling cout <<

 
 
John
Guest
Posts: n/a
 
      11-23-2004
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:rocess_time_out(CChatSocket* pSocket)
{
pSocket->Shutdown();
pSocket->Close();
pSocket->Release();
cout << "socket disconnected becasue of timeout" << endl; // *** line C
}



Thank,

John


 
Reply With Quote
 
 
 
 
John
Guest
Posts: n/a
 
      11-23-2004
This is a stupid question, becasue the earse() happens in another thread
(Win32 Timer thread) rather than the allocate thread. Sorry for that.


"John" <(E-Mail Removed)> wrote in message
news:cnuep2$jnm$(E-Mail Removed)...
> 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:rocess_time_out(CChatSocket* pSocket)
> {
> pSocket->Shutdown();
> pSocket->Close();
> pSocket->Release();
> cout << "socket disconnected becasue of timeout" << endl; // *** line
> C
> }
>
>
>
> Thank,
>
> John
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: cout vs std::cout Stefan Ram C++ 7 09-30-2008 01:55 PM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
can cout<<""; can cout<<""; contribute ? wangzhihuii@yahoo.com.cn C++ 4 09-15-2005 06:16 PM
std::cout vs cout Pmb C++ 2 06-02-2004 03:27 PM
man cout or info cout abi C++ 2 06-28-2003 06:42 PM



Advertisments