Fixing memory leak with RefPtr

This commit is contained in:
Dynamitos
2020-04-01 02:17:49 +02:00
parent 62c2d37cb3
commit 3ba8f2c2a0
37 changed files with 1675 additions and 270 deletions
+36 -6
View File
@@ -1,5 +1,7 @@
#pragma once
#include "MinimalEngine.h"
#include "EngineTypes.h"
#include <initializer_list>
#include <iterator>
#include <assert.h>
#ifndef DEFAULT_ALLOC_SIZE
@@ -95,11 +97,15 @@ namespace Seele
_data = other._data;
other._data = nullptr;
}
return *this;
}
~Array()
{
free(_data);
_data = nullptr;
if(_data)
{
free(_data);
_data = nullptr;
}
}
template<typename X>
class IteratorBase {
@@ -132,7 +138,7 @@ namespace Seele
{
return p == other.p;
}
inline bool operator-(const IteratorBase& other)
inline int operator-(const IteratorBase& other)
{
return p - other.p;
}
@@ -140,17 +146,36 @@ namespace Seele
p++;
return *this;
}
IteratorBase& operator--() {
p--;
return *this;
}
IteratorBase operator++(int) {
IteratorBase tmp(*this);
++*this;
return tmp;
}
IteratorBase operator--(int) {
IteratorBase tmp(*this);
--*this;
return tmp;
}
private:
X* p;
};
typedef IteratorBase<T> Iterator;
typedef IteratorBase<const T> ConstIterator;
bool operator==(const Array& other)
{
return _data == other._data;
}
bool operator!=(const Array& other)
{
return !(*this == other);
}
Iterator find(const T& item)
{
for (int i = 0; i < arraySize; ++i)
@@ -179,8 +204,8 @@ namespace Seele
allocated = calculateGrowth(newSize);
void* tempArray = malloc(sizeof(T) * allocated);
assert(tempArray != nullptr);
std::memset(tempArray, 0, sizeof(T) * allocated);
std::memcpy(tempArray, _data, arraySize * sizeof(T));
memset(tempArray, 0, sizeof(T) * allocated);
delete _data;
_data = (T*)tempArray;
}
@@ -218,7 +243,8 @@ namespace Seele
}
else
{
T* newData = new T[newSize];
T* newData = (T*)malloc(newSize * sizeof(T));
assert(newData != nullptr);
allocated = newSize;
std::memcpy(newData, _data, sizeof(T) * arraySize);
arraySize = newSize;
@@ -243,6 +269,10 @@ namespace Seele
{
return _data[arraySize - 1];
}
void pop()
{
arraySize--;
}
T& operator[](int index) const
{
assert(index >= 0 && index < arraySize);