This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Problem with Map Iterator




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.





Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]