Fix memory leaks in localisation

Assigning a pointer to std::string appears to only perform a copy and
does not transfer ownership of the pointer, thus the allocated memory is
will never be freed.

Implement a method to construct an std::string from a StringBuilder to
avoid memory leaks when retreiving the contents of a StringBuilder and
storing it in a std::string.
This commit is contained in:
Øystein Dale 2018-07-17 23:37:56 +02:00
parent 34cf068650
commit 934e53869c
2 changed files with 17 additions and 2 deletions

View File

@ -10,6 +10,7 @@
#pragma once
#include <algorithm>
#include <string>
#include "../common.h"
#include "Math.hpp"
@ -132,6 +133,20 @@ public:
return result;
}
/**
* Returns the current string buffer as a standard string.
*/
std::string GetStdString() const
{
std::string result;
result.reserve(_length);
for (std::size_t i = 0U; i < _length; i++)
{
result.push_back(_buffer[i]);
}
return result;
}
/**
* Gets the current state of the StringBuilder. Warning: this represents the StringBuilder's current working buffer and will
* be deallocated when the StringBuilder is destructed.

View File

@ -389,7 +389,7 @@ private:
}
if (sb.GetLength() == 8)
{
_currentGroup = sb.GetString();
_currentGroup = sb.GetStdString();
_currentObjectOverride = GetObjectOverride(_currentGroup);
_currentScenarioOverride = nullptr;
if (_currentObjectOverride == nullptr)
@ -432,7 +432,7 @@ private:
if (closedCorrectly)
{
_currentGroup = sb.GetString();
_currentGroup = sb.GetStdString();
_currentObjectOverride = nullptr;
_currentScenarioOverride = GetScenarioOverride(_currentGroup);
if (_currentScenarioOverride == nullptr)