From 0307a1d9be8eb2e818961a21be5ece299f0e198f Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Wed, 4 Apr 2018 07:31:49 +0200 Subject: [PATCH] Fix memory leaks in Map and Set --- src/containers/Map.h | 29 ++++++++++++++++------------- src/containers/Set.h | 29 ++++++++++++++++------------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/containers/Map.h b/src/containers/Map.h index cea9342c..518075aa 100644 --- a/src/containers/Map.h +++ b/src/containers/Map.h @@ -380,25 +380,28 @@ class Map { /// Copy constructor Map(const Map& map) :mNbUsedEntries(map.mNbUsedEntries), mNbFreeEntries(map.mNbFreeEntries), mCapacity(map.mCapacity), - mAllocator(map.mAllocator), mFreeIndex(map.mFreeIndex) { + mBuckets(nullptr), mEntries(nullptr), mAllocator(map.mAllocator), mFreeIndex(map.mFreeIndex) { - // Allocate memory for the buckets - mBuckets = static_cast(mAllocator.allocate(mCapacity * sizeof(int))); + if (mCapacity > 0) { - // Allocate memory for the entries - mEntries = static_cast(mAllocator.allocate(mCapacity * sizeof(Entry))); + // Allocate memory for the buckets + mBuckets = static_cast(mAllocator.allocate(mCapacity * sizeof(int))); - // Copy the buckets - std::memcpy(mBuckets, map.mBuckets, mCapacity * sizeof(int)); + // Allocate memory for the entries + mEntries = static_cast(mAllocator.allocate(mCapacity * sizeof(Entry))); - // Copy the entries - for (int i=0; i < mCapacity; i++) { + // Copy the buckets + std::memcpy(mBuckets, map.mBuckets, mCapacity * sizeof(int)); - new (&mEntries[i]) Entry(map.mEntries[i].hashCode, map.mEntries[i].next); + // Copy the entries + for (int i=0; i < mCapacity; i++) { - if (map.mEntries[i].keyValue != nullptr) { - mEntries[i].keyValue = static_cast*>(mAllocator.allocate(sizeof(Pair))); - new (mEntries[i].keyValue) Pair(*(map.mEntries[i].keyValue)); + new (&mEntries[i]) Entry(map.mEntries[i].hashCode, map.mEntries[i].next); + + if (map.mEntries[i].keyValue != nullptr) { + mEntries[i].keyValue = static_cast*>(mAllocator.allocate(sizeof(Pair))); + new (mEntries[i].keyValue) Pair(*(map.mEntries[i].keyValue)); + } } } } diff --git a/src/containers/Set.h b/src/containers/Set.h index 646507a1..64fbc657 100644 --- a/src/containers/Set.h +++ b/src/containers/Set.h @@ -379,25 +379,28 @@ class Set { /// Copy constructor Set(const Set& set) :mNbUsedEntries(set.mNbUsedEntries), mNbFreeEntries(set.mNbFreeEntries), mCapacity(set.mCapacity), - mAllocator(set.mAllocator), mFreeIndex(set.mFreeIndex) { + mBuckets(nullptr), mEntries(nullptr), mAllocator(set.mAllocator), mFreeIndex(set.mFreeIndex) { - // Allocate memory for the buckets - mBuckets = static_cast(mAllocator.allocate(mCapacity * sizeof(int))); + if (mCapacity > 0) { - // Allocate memory for the entries - mEntries = static_cast(mAllocator.allocate(mCapacity * sizeof(Entry))); + // Allocate memory for the buckets + mBuckets = static_cast(mAllocator.allocate(mCapacity * sizeof(int))); - // Copy the buckets - std::memcpy(mBuckets, set.mBuckets, mCapacity * sizeof(int)); + // Allocate memory for the entries + mEntries = static_cast(mAllocator.allocate(mCapacity * sizeof(Entry))); - // Copy the entries - for (int i=0; i < mCapacity; i++) { + // Copy the buckets + std::memcpy(mBuckets, set.mBuckets, mCapacity * sizeof(int)); - new (&mEntries[i]) Entry(set.mEntries[i].hashCode, set.mEntries[i].next); + // Copy the entries + for (int i=0; i < mCapacity; i++) { - if (set.mEntries[i].value != nullptr) { - mEntries[i].value = static_cast(mAllocator.allocate(sizeof(V))); - new (mEntries[i].value) V(*(set.mEntries[i].value)); + new (&mEntries[i]) Entry(set.mEntries[i].hashCode, set.mEntries[i].next); + + if (set.mEntries[i].value != nullptr) { + mEntries[i].value = static_cast(mAllocator.allocate(sizeof(V))); + new (mEntries[i].value) V(*(set.mEntries[i].value)); + } } } }