Boost logo

Boost Users :

From: Kevin Heifner (heifnerk_at_[hidden])
Date: 2008-05-14 10:24:04


Igor R. wrote:
> >
> > The problem with this code is that you are returning a reference
> > to data that might be immediately invalidated. A push_back via
> > another thread can cause the referenced memory to no longer be valid.
>
> Does push_back() really invalidate iterators to *previous* vector elements?!

Yes, and I quote the spec for insert which push_back() is based
on: "Remarks: Causes reallocation if the new size is greater than
the old capacity. If no reallocation happens, all the iterators
and references before the insertion point remain valid."

So unless you set capacity of the vector so that it never grows
then push_back() can invalidate a held reference.

Also it probably should be noted that even if you setup capacity
so that the vector never grows and only allowed push_back() you
still need mutex guards around any access to the shared object.

KevinH

-- 
Kevin Heifner  heifner @ ociweb.com  http://heifner.blogspot.com
           Object Computing, Inc. (OCI) www.ociweb.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net