🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c++.moderated
Article #19

Re: STL loops through a list with one element more than once! Why?

#19
From: Terris Linenbach
Date: Mon, 01 Jan 1996 17:48
68 lines
2635 bytes
X-Original-Date: 1 Jan 1996 05:46:03 GMT

"erase" invalidates all active iterators on the container.  The results are 
undefined.

Do this instead:

_itemList.erase( _itemList.begin(), _itemList.end() );


- Terris

In article <4c1n8o$2jn@netlab.cs.rpi.edu>, wiseb@cs.rpi.edu says...
>
>X-Original-Date: Fri, 29 Dec 1995 16:21:37 -0800
>
>Hi,
>
>I am using the STL to hold a list of items for a DDEML topic.  When
>the topic is destroyed, all of the associated items with that
>topic are erased from the topic's item list.  Erasing the item
>also causes the item's destructor to be called:
>
>
>BOOL DDETopic::DelAllItems ()
>{
>#ifdef DEBUGON
>Tracer t("DDETopic::DelAllItems ()");
>#endif
>
>   for (_itemIter  =   _itemList.begin();
>        _itemIter  !=  _itemList.end();
>        _itemIter++)
>   {
>#ifdef DEBUGON
>if (__theLog) __theLog->OutputLn ("calling erase for item");
>#endif
>      _itemList.erase (_itemIter);
>   }
>
>   return TRUE;
>}
>
>In my particular application the list only contains a single 
>element.
>
>When DEBUGON is enabled, the single item is correctly deleted the
>first time through the loop.  However, I can also verify that
>the loop is entered a second time, thereby resulting in a GPF.
>
>Any ideas why the _itemIter != _itemList.end() test of the for
>loop is failing?  Anyone else encountered this?
>
>Bowden
>Dept of Computer Science, RPI
>wiseb@cs.rpi.edu
>http://www.cs.rpi.edu/~wiseb/
>
>    [ comp.lang.c++.moderated is a moderated newsgroup.  Submit articles ]
>    [  to <c++-submit@netlab.cs.rpi.edu>.  The moderation policy can be  ]
>    [   retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>.   ]
>    [    Moderators can be reached at: c++-request@netlab.cs.rpi.edu.    ]


    [ comp.lang.c++.moderated is a moderated newsgroup.  Submit articles ]
    [  to <c++-submit@netlab.cs.rpi.edu>.  The moderation policy can be  ]
    [   retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>.   ]
    [    Moderators can be reached at: c++-request@netlab.cs.rpi.edu.    ]
Technical Details
Message-ID: <4c96ot$1ve@netlab.cs.rpi.edu>
Path: arc1-ca.pugleaf.net!news.kei.com!ub!newserve!rebecca!rpi!not-for-mail
References: <4c1n8o$2jn@netlab.cs.rpi.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.kei.com!ub!newserve!rebecca!rpi!not-for-mail
From: terris@rahul.net (Terris Linenbach)
Newsgroups: comp.lang.c++.moderated,comp.lang.c++,comp.os.ms-windows.programmer.misc
Subject: Re: STL loops through a list with one element more than once!  Why?
Date: 1 Jan 1996 17:48:13 -0000
Organization: a2i network
Sender: cppmods@netlab.cs.rpi.edu
Approved: Dietmar.Kuehl@uni-konstanz.de
Message-ID: <4c96ot$1ve@netlab.cs.rpi.edu>
References: <4c1n8o$2jn@netlab.cs.rpi.edu>
NNTP-Posting-Host: netlab.cs.rpi.edu