Browse Source

Fix leaks and oddities with water Array class

Signed-off-by: falkTX <falktx@gmail.com>
tags/v2.1-alpha1-winvst
falkTX 5 years ago
parent
commit
b2c3cccc2b
2 changed files with 32 additions and 26 deletions
  1. +1
    -1
      source/modules/water/containers/Array.h
  2. +31
    -25
      source/modules/water/containers/ArrayAllocationBase.h

+ 1
- 1
source/modules/water/containers/Array.h View File

@@ -120,7 +120,7 @@ public:
/** Destructor. */
~Array() noexcept
{
clear();
deleteAllElements();
}
/** Copies another array.


+ 31
- 25
source/modules/water/containers/ArrayAllocationBase.h View File

@@ -3,7 +3,7 @@
This file is part of the Water library.
Copyright (c) 2016 ROLI Ltd.
Copyright (C) 2017-2018 Filipe Coelho <falktx@falktx.com>
Copyright (C) 2017-2019 Filipe Coelho <falktx@falktx.com>
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 <typename T = ElementType>
NonTriviallyCopyableBool<T> setAllocatedSize (const size_t numNewElements) noexcept
{
if (numAllocated != numNewElements)
if (numAllocated == numNewElements)
return true;
if (numNewElements > 0)
{
if (numNewElements > 0)
{
HeapBlock<ElementType> newElements;
HeapBlock<ElementType> 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();
}
}


Loading…
Cancel
Save