473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

list iterator is invalid after calling cout <<

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
1 1726
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Barry Hynes | last post by:
Hi folks, still foolin with SafeList... anyhow why does the following code return the wrong size for SL2 any help greatly appreciated Thanks: Barry
3
by: aquanutz | last post by:
Ok, I have a list of strings (list<string> stringList) that I want to sort alphabetcially, only "sort(stringList.begin(), stringList.end()); ) does not work. Any insight would be helpful. Thanks!
6
by: PengYu.UT | last post by:
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't...
9
by: alopatenko | last post by:
I have a template class template <Class W> class WS At some point I have to use a STL list WS<W>objects so, I define #include <list>
3
by: janzon | last post by:
Hi! Sorry for the bad subject line... Here's what I mean. Suppose we deal with C++ standard integers lists (the type is indifferent). We have a function f, declared as list<intf(int); Now...
1
by: cakeathon | last post by:
I'm working my way through the accelerated C++ book, and exercise 10-5, & 10-6 have me stuck I have the follwing class in a header file: class String_list { public: String_list(); void...
10
by: arnuld | last post by:
It is quite an ugly hack but it is all I am able to come up with for now :-( and it does the requires work. I want to improve the program, I know you people have much better ideas ;-) /* C++...
9
by: subramanian100in | last post by:
Consider the program x.cpp : #include <cstdlib> #include <iostream> #include <list> #include <map> using namespace std; int main()
3
by: banangroda | last post by:
Compilation fails at "line.insert(line.end(), x.begin(), i);" and I can't figure out why. Here is the code: /* 5-1. Design and implement a program to produce a permuted index. A permuted index...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.