Skip to content

Commit

Permalink
fix vector memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jxy-s committed Oct 13, 2024
1 parent fec67e6 commit 75ac63b
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions radiant/detail/VectorOperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ namespace detail
template <typename T, typename TAllocator>
struct VectorAlloc
{
public:

~VectorAlloc()
{
if (buffer)
Expand All @@ -42,17 +44,19 @@ struct VectorAlloc
}

explicit VectorAlloc(TAllocator& alloc) noexcept
: allocator(alloc),
buffer(nullptr),
: buffer(nullptr),
size(0),
capacity(0)
capacity(0),
allocator(alloc)
{
}

void Clear();

bool Alloc(uint32_t count)
{
RAD_ASSERT(buffer == nullptr);

buffer = allocator.Alloc(count);
if (!buffer)
{
Expand All @@ -73,10 +77,13 @@ struct VectorAlloc
return tmp;
}

TAllocator& allocator;
T* buffer;
uint32_t size;
uint32_t capacity;

private:

TAllocator& allocator;
};

template <typename T>
Expand Down Expand Up @@ -401,8 +408,7 @@ struct VectorStorage<T, TInlineCount, false>
}

VectorAlloc<ValueType, TAllocator> vec(alloc);
vec.buffer = vec.allocator.Alloc(m_size);
if (!vec.buffer)
if (!vec.Alloc(m_size))
{
return Error::NoMemory;
}
Expand Down Expand Up @@ -523,8 +529,7 @@ struct VectorStorage<T, TInlineCount, true>
else
{
VectorAlloc<ValueType, TAllocator> vec(alloc);
vec.buffer = vec.allocator.Alloc(m_size);
if (!vec.buffer)
if (!vec.Alloc(m_size))
{
return Error::NoMemory;
}
Expand Down Expand Up @@ -866,6 +871,8 @@ struct VectorOperations : public VectorStorage<T, TInlineCount>
}
else
{
Free(alloc);

m_data = vec.Release();
m_capacity = count;
}
Expand Down Expand Up @@ -895,15 +902,16 @@ struct VectorOperations : public VectorStorage<T, TInlineCount>
else
{
VectorAlloc<ValueType, TAllocator> vec(alloc);
vec.buffer = vec.allocator.Alloc(count);
if (!vec.buffer)
if (!vec.Alloc(count))
{
return Error::NoMemory;
}

ManipType().CopyCtor(vec.buffer, count, value);
ManipType().DtorRange(Data(), Data() + m_size);

Free(alloc);

m_data = vec.Release();
m_capacity = count;
}
Expand Down Expand Up @@ -942,6 +950,8 @@ struct VectorOperations : public VectorStorage<T, TInlineCount>
}
else
{
Free(alloc);

m_data = vec.Release();
m_capacity = span.Size();
}
Expand Down

0 comments on commit 75ac63b

Please sign in to comment.