Question about iterator management in sw/source/core/fields/cellfml.cxx

Stephan Bergmann sbergman at redhat.com
Fri Feb 3 05:01:42 PST 2012


On 02/02/2012 09:08 PM, julien2412 wrote:
> Would this patch better ? (I kept the for loop)

Unfortunately that still has a problem.  After "rBoxes.erase(toErase)", 
"it" (which is the same as "toErase") is invalidated, so incrementing it 
(up in the for(...;...;...) part) has undefined behavior.

The "standard idiom" is

   for (iterator i = m.begin(); i != m.end();) {
     if (doErase) {
       m.erase(i++);
     } else {
       ++i;
     }
   }

> diff --git a/sw/source/core/fields/cellfml.cxx
> b/sw/source/core/fields/cellfml.cxx
> index 33b953e..5c626dd 100644
> --- a/sw/source/core/fields/cellfml.cxx
> +++ b/sw/source/core/fields/cellfml.cxx
> @@ -967,8 +967,8 @@ void SwTableFormula::GetBoxes( const SwTableBox&
> rSttBox,
>
>                   if( pTbl->IsHeadline( *pLine ) )
>                   {
> -                    rBoxes.erase( it++ );
> -                    --it;
> +                    SwSelBoxes::iterator toErase = it;
> +                    rBoxes.erase( toErase );
>                   }
>               }
>           } while( sal_False );
>
> I read that to erase a position on a iterator invalidate this iterator from
> position to the end
> (http://www.cplusplus.com/reference/stl/vector/erase/)
> I don't know if to create this temporary variable "toErase" change something
> or it's all the same.

The text you cite is about vector, for which *all* iterators into a 
vector are invalidated upon an erase.  For map (which SwSelBoxes is), 
only iterators pointing to the erased element are invalidated.

> (sorry for the nitpicking but I'd like to understand this point)

No problem at all, and I hope my nitpicking helps shed some light.

Stephan


More information about the LibreOffice mailing list