Add reserve before the creation of a new attribute, copy of an old one: solve a bug for the index version of CMap/GMap when the underlying vector is reallocated

This commit is contained in:
Guillaume Damiand 2023-03-10 11:30:07 +01:00
parent 477f71b876
commit 72b356d309
2 changed files with 16 additions and 0 deletions

View File

@ -566,6 +566,15 @@ void test_split_attribute_functor_one_dart
Attribute_descriptor_i a1 = amap.template attribute<i>(adart);
if ( found_attributes.is_defined(a1) )
{ // Here the attribute was already present in the hash_map
// We need to call reserve for the cc with index case. Indeed, if the vector
// is reallocated, the reference returned by get_attribute<i>(a1) will be
// invalidated, and the copy will be wrong. Note that there is no overhead
// since the creation of the attribute need one allocation.
amap.template attributes<i>().reserve(amap.template attributes<i>().size()+1);
// Now we are sure that the creation of a new attribute will not imply
// a realloc.
Attribute_descriptor_i a2 = amap.template
create_attribute<i>(amap.template get_attribute<i>(a1));

View File

@ -293,6 +293,13 @@ void GMap_test_split_attribute_functor_one_dart
Attribute_descriptor_i a1 = amap.template attribute<i>(adart);
if ( found_attributes.is_defined(a1) )
{ // Here the attribute was already present in the hash_map
// We need to call reserve for the cc with index case. Indeed, if the vector
// is reallocated, the reference returned by get_attribute<i>(a1) will be
// invalidated, and the copy will be wrong. Note that there is no overhead
// since the creation of the attribute need one allocation.
amap.template attributes<i>().reserve(amap.template attributes<i>().size()+1);
Attribute_descriptor_i a2 = amap.template
create_attribute<i>(amap.template get_attribute<i>(a1));