473,507 Members | 13,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL list::erase() weird action going on

Today I experienced something very very weird with STL.
The code snippet below should work, but it doesn't with Microsoft VC++ 6.0.

Ok what I have is a list of network connections. The code below removes one
of these connections from a temporary list. It goes through all the items to
find the right connection and removes it by erasing with the matching
iterator.

However what happens is that the item stays in the list after beeing deleted
but another one is deleted though. The size of the list goes down by 1 after
the erase function but the item still stays there. This was verified by
printing out the list after the erase function was called.

Can anyone explain this? My first thought is that it must be a bug in the
compiler.

I solved it by using list::remove() instead.

The list is of type list<CClientConnection *>

void CConnectionList::RemoveConnectionFromInProcessList (CClientConnection
*pC)
{
CConnectionList::iterator it;

// Remove connection from the temporary list
for (it = m_InProcessList.begin(); it != m_InProcessList.end(); it++)
{
if (*it = pC)
{
m_InProcessList.erase(it);
break;
}
}
}

Thanks in advance.
-- John
Jul 22 '05 #1
7 1759
"John Smith" <jo********@x-formation.com> wrote in message
news:41*********************@dread11.news.tele.dk. ..
Today I experienced something very very weird with STL.
The code snippet below should work, but it doesn't with Microsoft VC++
6.0. .... However what happens is that the item stays in the list after beeing
deleted
but another one is deleted though. The size of the list goes down by 1
after
the erase function but the item still stays there. This was verified by
printing out the list after the erase function was called.

Can anyone explain this? My first thought is that it must be a bug in the
compiler.

I solved it by using list::remove() instead.

The list is of type list<CClientConnection *>

void CConnectionList::RemoveConnectionFromInProcessList (CClientConnection
*pC)
{
CConnectionList::iterator it;

// Remove connection from the temporary list
for (it = m_InProcessList.begin(); it != m_InProcessList.end(); it++)
{
if (*it = pC) You probably intend to write:
if( *it == pC )
{
m_InProcessList.erase(it);
break;
}
}
}


I hope this helps ;)
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 22 '05 #2
> You probably intend to write:
if( *it == pC )

I hope this helps ;)
Ivan


Hehe crap on me ;)
You know... I spent like 6 hours straight to debug and find this pieces of
code. I went through alot of classes and even more threads which uses this
code in parallel. I had many theories about bad mutexes which were
destroying and overlapping the runtime sequence... all to realise it was
such a simple mistake.

Anyway... thanks alot ;)

-- John
Jul 22 '05 #3
> if (*it = pC)

maybe: if (*it == pC) ?
Jul 22 '05 #4
On Thu, 23 Dec 2004 23:02:08 +0100, John Smith wrote:
You probably intend to write:
if( *it == pC )

I hope this helps ;)
Ivan


Hehe crap on me ;)
You know... I spent like 6 hours straight to debug and find this pieces of
code.


Doesn't VC++6 emit a warning for this code? I'm in the habit of leaving
warnings turned up pretty high and then making sure that all code
compiles warning-free, so that stuff like this doesn't get lost in a
bunch of warnings I know about but don't care to fix.

--
Greg Schmidt gr***@trawna.com
Trawna Publications http://www.trawna.com/
Jul 22 '05 #5
Doesn't VC++6 emit a warning for this code? I'm in the habit of leaving
warnings turned up pretty high and then making sure that all code
compiles warning-free, so that stuff like this doesn't get lost in a
bunch of warnings I know about but don't care to fix.


No it did not. The normal level is the 2nd highest (/W3) and the highest
(/W4) is quite useless since it gives over 500 warnings from Microsofts own
STL code.
Normally my code is always warning-free this one was just a slip-through.
Oh well... bug fixed and lesson learned.

-- John
Jul 22 '05 #6
Here is a quote I like:

"Chess mastery is a discipline which places a premium on consistency:
One moment of hallucination can throw away hours of good work.

Someone once asked a grandmaster how to avoid such mistakes, and was
told, "Sit on your hands!"

As with the chess master, the master programmer eventually --- usually
through painful experience -- acquires the habit of stepping back from
each involved coding task once done, clearing the mind, and re-reading
thoroughly with fresh eyes, looking for elementary mistakes.

It is not a natural habit, but once acquired it can make programming
much more pleasant. You may even eventually find your code working
first try on a regular basis, an experience akin to nirvana."
--
P.Krumins

Jul 22 '05 #7
> It is not a natural habit, but once acquired it can make programming
much more pleasant. You may even eventually find your code working
first try on a regular basis, an experience akin to nirvana."
--


Actually I managed to solve other bugs at the same time. There were some
overflow bugs which I wouldn't have found otherwise that easily. So all in
all it wasn't all in vain because I refactored and improve the hard-to-read
parts of the code. We're talking about atleast 1500 lines of code I had to
go through and debug (the code related to this problem) so it had to take
some time.

-- John
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
20472
by: Paras | last post by:
Hi What is the correct way to delete an element from STL list while iterating through the list list<A*> _ls; A * a; list<A*>::iterator si1;
2
9946
by: s | last post by:
Here is a snippet of my code: <code> list< MyClass * >outgoing_pool; MyClass* GetWaitingObject(string freq) { MyClass *temp_ptr = NULL; list<MyClass*>::iterator i;
3
1523
by: techmaccoy | last post by:
I am trying to access a entry under HKEY_Current_USER/Software. In My code it List seven subkeys under there. Actually there are lot more and the entry i wanted to access in seen in the Registry,...
3
6081
by: Dalbosco J-F | last post by:
Hi, Sorry if this has already been answered. Given a std::list and a reverse_iterator is there a way to erase the element pointed to by the reverse_iterator via the erase method? Apparently...
6
11878
by: catphive.lists | last post by:
Is there a way to call erase(iter) on a list without invalidating the iterator? Can I make a copy of an iterator and then move forward the original without moving the copy? I'm aware of the...
9
6691
by: Krice | last post by:
I get "list erase iterator outside range" message from MSVC with this function: int Level::Remove_Npc(Game_Object *o) { list <Game_Object *>::iterator pos; Game_Object *c; for...
0
7109
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7313
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
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7481
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...
1
5039
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...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3190
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1537
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.