Fix: Crash when reininitializing empty generic array

Noticed this while developing new code that used GArray.
This commit is contained in:
Hans Goudey 2022-10-04 22:11:02 -05:00
parent b804f925c7
commit fbbd7f0d5c
2 changed files with 12 additions and 1 deletions

View File

@ -231,7 +231,9 @@ class GArray {
this->deallocate(new_data);
throw;
}
this->deallocate(data_);
if (this->data_) {
this->deallocate(data_);
}
data_ = new_data;
}

View File

@ -114,4 +114,13 @@ TEST(generic_array, InContainer)
}
}
TEST(generic_array, ReinitEmpty)
{
GArray<> array(CPPType::get<int>());
array.reinitialize(10);
array.as_mutable_span().typed<int>()[9] = 7;
EXPECT_EQ(array.size(), 10);
EXPECT_EQ(array.as_span().typed<int>()[9], 7);
}
} // namespace blender::tests