From fea3e44856afd9db504af9ef122d382105926c8d Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Wed, 4 Apr 2018 18:37:24 +0200 Subject: [PATCH] Fix memory leak in List, Map and Set --- src/containers/List.h | 15 +++++++++++++-- src/containers/Map.h | 24 +++++++++++++++++++----- src/containers/Set.h | 24 +++++++++++++++++++----- 3 files changed, 51 insertions(+), 12 deletions(-) mode change 100644 => 100755 src/containers/List.h mode change 100644 => 100755 src/containers/Map.h mode change 100644 => 100755 src/containers/Set.h diff --git a/src/containers/List.h b/src/containers/List.h old mode 100644 new mode 100755 index 410e1746..a601e7c3 --- a/src/containers/List.h +++ b/src/containers/List.h @@ -32,6 +32,7 @@ #include #include #include +#include namespace reactphysics3d { @@ -204,8 +205,18 @@ class List { if (mBuffer != nullptr) { - // Copy the elements to the new allocated memory location - std::memcpy(newMemory, mBuffer, mSize * sizeof(T)); + if (mSize > 0) { + + // Copy the elements to the new allocated memory location + T* destination = static_cast(newMemory); + T* items = static_cast(mBuffer); + std::uninitialized_copy(items, items + mSize, destination); + + // Destruct the previous items + for (size_t i=0; i 0) { + + // Copy the old entries to the new allocated memory location + std::uninitialized_copy(mEntries, mEntries + mNbUsedEntries, newEntries); + + // Destruct the old entries in the previous location + for (int i=0; i < mNbUsedEntries; i++) { + mEntries[i].~Entry(); + } + } // Construct the new entries for (int i=mNbUsedEntries; i(mAllocator.allocate(mCapacity * sizeof(Entry))); // Copy the buckets - std::memcpy(mBuckets, map.mBuckets, mCapacity * sizeof(int)); + std::uninitialized_copy(map.mBuckets, map.mBuckets + mCapacity, mBuckets); // Copy the entries for (int i=0; i < mCapacity; i++) { @@ -710,7 +724,7 @@ class Map { mEntries = static_cast(mAllocator.allocate(mCapacity * sizeof(Entry))); // Copy the buckets - std::memcpy(mBuckets, map.mBuckets, mCapacity * sizeof(int)); + std::uninitialized_copy(map.mBuckets, map.mBuckets + mCapacity, mBuckets); // Copy the entries for (int i=0; i < mCapacity; i++) { diff --git a/src/containers/Set.h b/src/containers/Set.h old mode 100644 new mode 100755 index 64fbc657..f770b51d --- a/src/containers/Set.h +++ b/src/containers/Set.h @@ -67,10 +67,16 @@ class Set { value = nullptr; } + /// Copy-constructor + Entry(const Entry& entry) { + hashCode = entry.hashCode; + next = entry.next; + value = entry.value; + } + /// Destructor ~Entry() { - assert(value == nullptr); } }; @@ -154,8 +160,16 @@ class Set { newBuckets[i] = -1; } - // Copy the old entries to the new allocated memory location - std::memcpy(newEntries, mEntries, mNbUsedEntries * sizeof(Entry)); + if (mNbUsedEntries > 0) { + + // Copy the old entries to the new allocated memory location + std::uninitialized_copy(mEntries, mEntries + mNbUsedEntries, newEntries); + + // Destruct the old entries at previous location + for (int i=0; i(mAllocator.allocate(mCapacity * sizeof(Entry))); // Copy the buckets - std::memcpy(mBuckets, set.mBuckets, mCapacity * sizeof(int)); + std::uninitialized_copy(set.mBuckets, set.mBuckets + mCapacity, mBuckets); // Copy the entries for (int i=0; i < mCapacity; i++) { @@ -651,7 +665,7 @@ class Set { mEntries = static_cast(mAllocator.allocate(mCapacity * sizeof(Entry))); // Copy the buckets - std::memcpy(mBuckets, set.mBuckets, mCapacity * sizeof(int)); + std::uninitialized_copy(set.mBuckets, set.mBuckets + mCapacity, mBuckets); // Copy the entries for (int i=0; i < mCapacity; i++) {