Cppcheck Dangerous iterator usage after erase()-method

Noel Grandin noelgrandin at gmail.com
Sun Sep 16 03:26:19 PDT 2012


On Sat, Sep 15, 2012 at 9:13 PM, Markus Mohrhard
<markus.mohrhard at googlemail.com> wrote:
> Hey,
>
>
>>
>> Here are the lines:
>>     178 void SwBlink::FrmDelete( const SwRootFrm* pRoot )
>>     179 {
>>     180     for( SwBlinkList::iterator it = aList.begin(); it !=
>> aList.end(); )
>>     181     {
>>     182         if( pRoot == (*it).GetRootFrm() )
>>     183             aList.erase( it );
>>     184         else
>>     185             ++it;
>>     186     }
>>     187 }
>>
>> I must recognize, I don't understand how can it work above all if we go  in
>> the "if" since there's no increment.
>>
>> Any idea?
>
> It can't. Line 183 is supposed to be:
>
> aList.erase(it++);
>

No, it should be
   it = aList.erase(it);

Once you have called erase(), the iterator becomes invalid, so it must
be replaced by the iterator returned by erase(), which returns the
next valid position,


More information about the LibreOffice mailing list