Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacques Lucke <jacques@blender.org>2022-02-02 15:11:33 +0300
committerJacques Lucke <jacques@blender.org>2022-02-02 15:11:33 +0300
commitbf8597febe2020e654020ad60e8af35c635c1a9a (patch)
treea55a1cb1b4a1268b6a11ebdde228b1a031a90042 /source/blender/blenlib
parenta985f558a6eb16cd6f00b550712b86923ab33fd3 (diff)
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.
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r--source/blender/blenlib/BLI_vector_set.hh4
-rw-r--r--source/blender/blenlib/tests/BLI_vector_set_test.cc10
2 files changed, 14 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_vector_set.hh b/source/blender/blenlib/BLI_vector_set.hh
index 0aac96f93bc..ed744d09314 100644
--- a/source/blender/blenlib/BLI_vector_set.hh
+++ b/source/blender/blenlib/BLI_vector_set.hh
@@ -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 (...) {
diff --git a/source/blender/blenlib/tests/BLI_vector_set_test.cc b/source/blender/blenlib/tests/BLI_vector_set_test.cc
index c4016ca75e1..fe3130a846f 100644
--- a/source/blender/blenlib/tests/BLI_vector_set_test.cc
+++ b/source/blender/blenlib/tests/BLI_vector_set_test.cc
@@ -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