Skip to content

Commit

Permalink
datapack: free all elements on clear (#1251)
Browse files Browse the repository at this point in the history
* Fixed memory leak

When a pack was cleared or destroyed the String and Raw types could cause memory leaks. This happens when "position" is sitting at the end of the vector and can never get past the "if (pos >= elements.length())" statement. This means there is a memory leak in any plugin that clears/destroys a pack with strings and doesn't set the position to length-1 or less beforehand.

* datapack: Fix delete op on CDataPackType::Raw.

Co-authored-by: Kyle Sanderson <kyle.leet@gmail.com>
  • Loading branch information
hlstriker and KyleSanderson committed Apr 28, 2020
1 parent d42c304 commit d044b13
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions core/logic/CDataPack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ CDataPack::Free(CDataPack *pack)

void CDataPack::Initialize()
{
position = 0;

do
{
} while (this->RemoveItem());

elements.clear();
position = 0;
}

void CDataPack::ResetSize()
Expand Down Expand Up @@ -214,6 +215,7 @@ bool CDataPack::RemoveItem(size_t pos)
{
pos = position;
}

if (pos >= elements.length())
{
return false;
Expand All @@ -228,7 +230,7 @@ bool CDataPack::RemoveItem(size_t pos)
{
case CDataPackType::Raw:
{
delete elements[pos].pData.vval;
delete [] elements[pos].pData.vval;
break;
}

Expand Down

0 comments on commit d044b13

Please sign in to comment.