From d044b13ce484f78fcee7b2677c3ba3fbf174d39c Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 27 Apr 2020 21:09:13 -0400 Subject: [PATCH] datapack: free all elements on clear (#1251) * 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 --- core/logic/CDataPack.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/logic/CDataPack.cpp b/core/logic/CDataPack.cpp index 16b1a34d..d794e379 100644 --- a/core/logic/CDataPack.cpp +++ b/core/logic/CDataPack.cpp @@ -65,12 +65,13 @@ CDataPack::Free(CDataPack *pack) void CDataPack::Initialize() { + position = 0; + do { } while (this->RemoveItem()); elements.clear(); - position = 0; } void CDataPack::ResetSize() @@ -214,6 +215,7 @@ bool CDataPack::RemoveItem(size_t pos) { pos = position; } + if (pos >= elements.length()) { return false; @@ -228,7 +230,7 @@ bool CDataPack::RemoveItem(size_t pos) { case CDataPackType::Raw: { - delete elements[pos].pData.vval; + delete [] elements[pos].pData.vval; break; }