http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60371

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Дилян Палаузов from comment #3)
> Indeed, adding
> 
>   z (const z& x) { var = strdup (x.var); }
> 
> solves the problem.  However, I don't understand how that "y.clear();"
> between the y.emplace_back() in the original program avoids the double free.

In the original program the vector is resized on the second insertion, so the
existing element must be copied to the new storage (which results in a shallow
copy of the malloc'd memory, and leads to a double free).

When you clear the vector it doesn't need to be resized, so no element is
copied, so no shallow copy.

Reply via email to