* Problem with Map Iterator
@ 2006-05-15 13:15 Sanjib Talukdar
2006-05-15 13:18 ` John Love-Jensen
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Sanjib Talukdar @ 2006-05-15 13:15 UTC (permalink / raw)
To: gcc-help
Hi,
I am getting a segmentation fault error while executing the following
code:
if (!this->colDtlsByName.empty())
{
std::map< std::string, CS_DATAFMT*,
std::less<std::string> >::iterator iterMap;
for (iterMap = this->colDtlsByName.begin();
iterMap != this->colDtlsByName.end(); ++iterMap)
{
CS_DATAFMT* obj = iterMap->second;
if (obj != NULL)
delete obj;
this->colDtlsByName.erase(iterMap);
}
}
The backtrace from gdb is as follows:
#0 0xff0e29b4 in std::_Rb_tree_increment () from
/usr/sfw/lib/libstdc++.so.6
#1 0x0009854c in std::_Rb_tree_iterator<std::pair<std::string
const, _cs_datafmt*> >::operator++ (this=0xfe77a0c4) at stl_tree.h:180
#2 0x0005a84c in MACDBQueryHandler::clearColInfo
(this=0xfe77bd30, inType=ALL)
at MACDBQueryHandler.C:123
The above code is in the method MACDBQueryHandler::clearColInfo() which
is being invoked in the class destructor.
We are using gcc version 3.4.3 on Solaris 10. The library libstdc++.so.6
is pointing to libstdc++.so.6.0.3.
Can anyone please point out the reason for the error?
Thank You.
Sanjib.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Problem with Map Iterator
2006-05-15 13:15 Problem with Map Iterator Sanjib Talukdar
@ 2006-05-15 13:18 ` John Love-Jensen
2006-05-15 13:22 ` Aseem Rastogi
2006-05-15 13:21 ` Burak Serdar
2006-05-15 13:22 ` Aseem Rastogi
2 siblings, 1 reply; 8+ messages in thread
From: John Love-Jensen @ 2006-05-15 13:18 UTC (permalink / raw)
To: Sanjib Talukdar, MSX to GCC
Hi Sanjib,
The erase(iterMap) invalidates all iterators into the map.
HTH,
--Eljay
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Problem with Map Iterator
2006-05-15 13:15 Problem with Map Iterator Sanjib Talukdar
2006-05-15 13:18 ` John Love-Jensen
@ 2006-05-15 13:21 ` Burak Serdar
2006-05-15 13:22 ` Aseem Rastogi
2 siblings, 0 replies; 8+ messages in thread
From: Burak Serdar @ 2006-05-15 13:21 UTC (permalink / raw)
To: Sanjib Talukdar; +Cc: gcc-help
As far as I know, map::erase invalidates the iterator, so incrementing
the iterator after erasing it is invalid.
On 5/15/06, Sanjib Talukdar <sanjibt@ascindia.com> wrote:
> Hi,
>
> I am getting a segmentation fault error while executing the following
> code:
>
> if (!this->colDtlsByName.empty())
> {
> std::map< std::string, CS_DATAFMT*,
> std::less<std::string> >::iterator iterMap;
> for (iterMap = this->colDtlsByName.begin();
> iterMap != this->colDtlsByName.end(); ++iterMap)
> {
> CS_DATAFMT* obj = iterMap->second;
> if (obj != NULL)
> delete obj;
> this->colDtlsByName.erase(iterMap);
> }
> }
>
> The backtrace from gdb is as follows:
>
> #0 0xff0e29b4 in std::_Rb_tree_increment () from
> /usr/sfw/lib/libstdc++.so.6
> #1 0x0009854c in std::_Rb_tree_iterator<std::pair<std::string
> const, _cs_datafmt*> >::operator++ (this=0xfe77a0c4) at stl_tree.h:180
> #2 0x0005a84c in MACDBQueryHandler::clearColInfo
> (this=0xfe77bd30, inType=ALL)
> at MACDBQueryHandler.C:123
>
> The above code is in the method MACDBQueryHandler::clearColInfo() which
> is being invoked in the class destructor.
> We are using gcc version 3.4.3 on Solaris 10. The library libstdc++.so.6
> is pointing to libstdc++.so.6.0.3.
>
> Can anyone please point out the reason for the error?
> Thank You.
> Sanjib.
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Problem with Map Iterator
2006-05-15 13:18 ` John Love-Jensen
@ 2006-05-15 13:22 ` Aseem Rastogi
2006-05-15 13:46 ` John Love-Jensen
0 siblings, 1 reply; 8+ messages in thread
From: Aseem Rastogi @ 2006-05-15 13:22 UTC (permalink / raw)
To: John Love-Jensen; +Cc: Sanjib Talukdar, MSX to GCC
Not all. Only the one pointed to by the iterator.
John Love-Jensen wrote:
>Hi Sanjib,
>
>The erase(iterMap) invalidates all iterators into the map.
>
>HTH,
>--Eljay
>
>
--
Nothing will work if u don't.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Problem with Map Iterator
2006-05-15 13:15 Problem with Map Iterator Sanjib Talukdar
2006-05-15 13:18 ` John Love-Jensen
2006-05-15 13:21 ` Burak Serdar
@ 2006-05-15 13:22 ` Aseem Rastogi
2006-05-15 14:04 ` Peter Doerfler
2 siblings, 1 reply; 8+ messages in thread
From: Aseem Rastogi @ 2006-05-15 13:22 UTC (permalink / raw)
To: Sanjib Talukdar; +Cc: gcc-help
could be because when u have erased the map node pointed to by iterMap,
iterMap++ crashes as the memory location pointed to by iterMap becomes
invalid.
so, instead of erasing in loop, do a clear () on map after the loop.
see inline.
hope this helps.
Sanjib Talukdar wrote:
>Hi,
>
>I am getting a segmentation fault error while executing the following
>code:
>
> if (!this->colDtlsByName.empty())
> {
> std::map< std::string, CS_DATAFMT*,
> std::less<std::string> >::iterator iterMap;
> for (iterMap = this->colDtlsByName.begin();
> iterMap != this->colDtlsByName.end(); ++iterMap)
> {
> CS_DATAFMT* obj = iterMap->second;
> if (obj != NULL)
> delete obj;
> this->colDtlsByName.erase(iterMap); ///////// Remove this line
> }
>
>>> call this->colDtlsByName.clear ();
>
> }
>
>The backtrace from gdb is as follows:
>
> #0 0xff0e29b4 in std::_Rb_tree_increment () from
>/usr/sfw/lib/libstdc++.so.6
> #1 0x0009854c in std::_Rb_tree_iterator<std::pair<std::string
>const, _cs_datafmt*> >::operator++ (this=0xfe77a0c4) at stl_tree.h:180
> #2 0x0005a84c in MACDBQueryHandler::clearColInfo
>(this=0xfe77bd30, inType=ALL)
> at MACDBQueryHandler.C:123
>
>The above code is in the method MACDBQueryHandler::clearColInfo() which
>is being invoked in the class destructor.
>We are using gcc version 3.4.3 on Solaris 10. The library libstdc++.so.6
>is pointing to libstdc++.so.6.0.3.
>
>Can anyone please point out the reason for the error?
>Thank You.
>Sanjib.
>
--
Nothing will work if u don't.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Problem with Map Iterator
2006-05-15 13:22 ` Aseem Rastogi
@ 2006-05-15 13:46 ` John Love-Jensen
0 siblings, 0 replies; 8+ messages in thread
From: John Love-Jensen @ 2006-05-15 13:46 UTC (permalink / raw)
To: Aseem Rastogi; +Cc: Sanjib Talukdar, MSX to GCC
Hi Aseem,
> Not all. Only the one pointed to by the iterator.
I stand corrected. Thanks.
Sincerely,
--Eljay
vector
all vector iterators are invalidated on any change
string & wstring
all string iterators are invalidated on any change
deque
all deque iterators are invalidated on any change, except for beginning and
end removals
list
no list iterators are invalidated, except for those referring to a deleted
element
set & multiset
no iterators are invalidated except ones pointing to a removed element
map & multimap
no iterators are invalidated except ones pointed to a removed element
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Problem with Map Iterator
2006-05-15 13:22 ` Aseem Rastogi
@ 2006-05-15 14:04 ` Peter Doerfler
2006-05-21 7:58 ` Dima Sorkin
0 siblings, 1 reply; 8+ messages in thread
From: Peter Doerfler @ 2006-05-15 14:04 UTC (permalink / raw)
To: Aseem Rastogi; +Cc: Sanjib Talukdar, gcc-help
Aseem Rastogi wrote:
> could be because when u have erased the map node pointed to by iterMap,
> iterMap++ crashes as the memory location pointed to by iterMap becomes
> invalid.
>
> so, instead of erasing in loop, do a clear () on map after the loop.
>
Alternatively, if you can't do that you can increment first and do the
erase() then. This creates a bit of overhead and a while loop is more
appropriate -- along these lines:
while (iterMap != iterMapEnd) {
CS_DATAFMT* obj = iterMap->second;
if (obj != NULL)
delete obj;
this->colDtlsByName.erase(iterMap++);
}
In your example, where you actually erase all nodes, the clear() method
is more appropriate, but if you need to remove nodes conditionally, the
above is an option.
I also seem to recall reading that std::map::erase will return an
iterator pointing to the next element in the future which would simplify
things.... Found it: It's on the v7 branch.
Best, Peter
> see inline.
>
> hope this helps.
>
> Sanjib Talukdar wrote:
>
>> Hi,
>>
>> I am getting a segmentation fault error while executing the following
>> code:
>>
>> if (!this->colDtlsByName.empty())
>> {
>> std::map< std::string, CS_DATAFMT*,
>> std::less<std::string> >::iterator iterMap; for (iterMap =
>> this->colDtlsByName.begin();
>> iterMap != this->colDtlsByName.end(); ++iterMap)
>> {
>> CS_DATAFMT* obj = iterMap->second;
>> if (obj != NULL)
>> delete obj;
>> this->colDtlsByName.erase(iterMap); /////////
>> Remove this line
>> }
>>
>
> >>> call this->colDtlsByName.clear ();
>
>>
>> }
>>
>> The backtrace from gdb is as follows:
>>
>> #0 0xff0e29b4 in std::_Rb_tree_increment () from
>> /usr/sfw/lib/libstdc++.so.6
>> #1 0x0009854c in std::_Rb_tree_iterator<std::pair<std::string
>> const, _cs_datafmt*> >::operator++ (this=0xfe77a0c4) at stl_tree.h:180
>> #2 0x0005a84c in MACDBQueryHandler::clearColInfo
>> (this=0xfe77bd30, inType=ALL)
>> at MACDBQueryHandler.C:123
>>
>> The above code is in the method MACDBQueryHandler::clearColInfo() which
>> is being invoked in the class destructor.
>> We are using gcc version 3.4.3 on Solaris 10. The library libstdc++.so.6
>> is pointing to libstdc++.so.6.0.3.
>>
>> Can anyone please point out the reason for the error?
>> Thank You.
>> Sanjib.
>>
>
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Problem with Map Iterator
2006-05-15 14:04 ` Peter Doerfler
@ 2006-05-21 7:58 ` Dima Sorkin
0 siblings, 0 replies; 8+ messages in thread
From: Dima Sorkin @ 2006-05-21 7:58 UTC (permalink / raw)
To: gcc-help
Hi.
There is an item exactly on this issue (deleting iterators) in
S.Meyers "Effective STL". I think there was something about incrementing
too.
Regards,
Dima.
On 5/15/06, Peter Doerfler wrote:
>
>
> Aseem Rastogi wrote:
> > could be because when u have erased the map node pointed to by iterMap,
> > iterMap++ crashes as the memory location pointed to by iterMap becomes
> > invalid.
> >
> > so, instead of erasing in loop, do a clear () on map after the loop.
> >
>
> Alternatively, if you can't do that you can increment first and do the
> erase() then. This creates a bit of overhead and a while loop is more
> appropriate -- along these lines:
>
> while (iterMap != iterMapEnd) {
> CS_DATAFMT* obj = iterMap->second;
> if (obj != NULL)
> delete obj;
> this->colDtlsByName.erase(iterMap++);
> }
>
> In your example, where you actually erase all nodes, the clear() method
> is more appropriate, but if you need to remove nodes conditionally, the
> above is an option.
>
> I also seem to recall reading that std::map::erase will return an
> iterator pointing to the next element in the future which would simplify
> things.... Found it: It's on the v7 branch.
>
> Best, Peter
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2006-05-21 7:58 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-05-15 13:15 Problem with Map Iterator Sanjib Talukdar
2006-05-15 13:18 ` John Love-Jensen
2006-05-15 13:22 ` Aseem Rastogi
2006-05-15 13:46 ` John Love-Jensen
2006-05-15 13:21 ` Burak Serdar
2006-05-15 13:22 ` Aseem Rastogi
2006-05-15 14:04 ` Peter Doerfler
2006-05-21 7:58 ` Dima Sorkin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).