Quellcode durchsuchen

storage: free memory in move assignment operator (with tests)

Michele Caini vor 4 Jahren
Ursprung
Commit
0fe73fb6ed
2 geänderte Dateien mit 12 neuen und 2 gelöschten Zeilen
  1. 3 0
      src/entt/entity/storage.hpp
  2. 9 2
      test/entt/entity/storage.cpp

+ 3 - 0
src/entt/entity/storage.hpp

@@ -314,11 +314,14 @@ public:
      * @return This sparse set.
      */
     basic_storage & operator=(basic_storage &&other) ENTT_NOEXCEPT {
+        maybe_resize_packed(0u);
+
         allocator = std::move(other.allocator);
         bucket_allocator = std::move(other.bucket_allocator);
         packed = std::exchange(other.packed, bucket_alloc_pointer{});
         bucket = std::exchange(other.bucket, 0u);
         count = std::exchange(other.count, 0u);
+
         return *this;
     }
 

+ 9 - 2
test/entt/entity/storage.cpp

@@ -91,9 +91,16 @@ TEST(Storage, Functionalities) {
 
     ASSERT_EQ(pool.capacity(), 0u);
 
-    (void)entt::storage<int>{std::move(pool)};
-    entt::storage<int> other;
+    entt::storage<int> other{std::move(pool)};
+
+    pool = std::move(other);
+
+    other = entt::storage<int>{};
+    other.emplace(entt::entity{3}, 3);
     other = std::move(pool);
+
+    ASSERT_EQ(pool.capacity(), 0u);
+    ASSERT_EQ(other.capacity(), 0u);
 }
 
 TEST(Storage, EmptyType) {