From b2c3cccc2b5697ed8ef298eaa1d1816fe52126dd Mon Sep 17 00:00:00 2001 From: falkTX Date: Mon, 28 Jan 2019 00:14:46 +0100 Subject: [PATCH] Fix leaks and oddities with water Array class Signed-off-by: falkTX --- source/modules/water/containers/Array.h | 2 +- .../water/containers/ArrayAllocationBase.h | 56 ++++++++++--------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/source/modules/water/containers/Array.h b/source/modules/water/containers/Array.h index 1205b7ceb..a5dbb63a2 100644 --- a/source/modules/water/containers/Array.h +++ b/source/modules/water/containers/Array.h @@ -120,7 +120,7 @@ public: /** Destructor. */ ~Array() noexcept { - clear(); + deleteAllElements(); } /** Copies another array. diff --git a/source/modules/water/containers/ArrayAllocationBase.h b/source/modules/water/containers/ArrayAllocationBase.h index 34a5c50e1..adba31515 100644 --- a/source/modules/water/containers/ArrayAllocationBase.h +++ b/source/modules/water/containers/ArrayAllocationBase.h @@ -3,7 +3,7 @@ This file is part of the Water library. Copyright (c) 2016 ROLI Ltd. - Copyright (C) 2017-2018 Filipe Coelho + Copyright (C) 2017-2019 Filipe Coelho Permission is granted to use this software under the terms of the ISC license http://www.isc.org/downloads/software-support-policy/isc-license/ @@ -131,38 +131,40 @@ public: template NonTriviallyCopyableBool setAllocatedSize (const size_t numNewElements) noexcept { - if (numAllocated != numNewElements) + if (numAllocated == numNewElements) + return true; + + if (numNewElements > 0) { - if (numNewElements > 0) - { - HeapBlock newElements; + HeapBlock newElements; - if (! newElements.malloc (numNewElements)) - return false; + if (! newElements.malloc (numNewElements)) + return false; - for (size_t i = 0; i < numNewElements; ++i) - { - if (i < numAllocated) - { - new (newElements + i) ElementType (std::move (elements[i])); - elements[i].~ElementType(); - } - else - { - new (newElements + i) ElementType (); - } - } - - elements = std::move (newElements); - } - else + size_t i = 0; + + for (; i < numNewElements; ++i) { - elements.free(); + if (i < numAllocated) + new (newElements + i) ElementType (std::move (elements[i])); + else + new (newElements + i) ElementType (); } - numAllocated = numNewElements; + for (; i < numAllocated; ++i) + elements[i].~ElementType(); + + elements = std::move (newElements); + } + else + { + for (size_t i = 0; i < numAllocated; ++i) + elements[i].~ElementType(); + + elements.free(); } + numAllocated = numNewElements; return true; } #endif @@ -233,6 +235,8 @@ public: ++target; ++source; } + + (--source)->~ElementType(); } else { @@ -242,6 +246,8 @@ public: --target; --source; } + + (++source)->~ElementType(); } }