BLI: fix memory leak in VectorSet

The leak happened when the vector set had to grow when it
was empty but it had allocated the keys array already.
This commit is contained in:
Jacques Lucke 2022-02-02 13:11:33 +01:00
parent a985f558a6
commit bf8597febe
2 changed files with 14 additions and 0 deletions

View File

@ -570,6 +570,10 @@ class VectorSet {
if (this->size() == 0) {
try {
slots_.reinitialize(total_slots);
if (keys_ != nullptr) {
this->deallocate_keys_array(keys_);
keys_ = nullptr;
}
keys_ = this->allocate_keys_array(usable_slots);
}
catch (...) {

View File

@ -271,4 +271,14 @@ TEST(vector_set, LookupKey)
EXPECT_EQ(set.lookup_key_ptr("a"), set.lookup_key_ptr_as("a"));
}
TEST(vector_set, GrowWhenEmpty)
{
/* Tests that the internal keys array is freed correctly when growing an empty set. */
VectorSet<int> set;
set.add(4);
set.remove(4);
EXPECT_TRUE(set.is_empty());
set.reserve(100);
}
} // namespace blender::tests