Fix memleak during WFL parsing (fixes #1680, fixes #1685)

This commit is contained in:
Celtic Minstrel 2017-05-11 03:57:15 -04:00
parent 33792a3b1e
commit 7b13d6ca33
2 changed files with 8 additions and 22 deletions

View file

@ -68,13 +68,9 @@ const char* const formula::id_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNO
formula::formula(const std::string& text, function_symbol_table* symbols)
: expr_()
, str_(text)
, symbols_(symbols)
, managing_symbols(symbols == nullptr)
, managed_symbols_(symbols ? nullptr : new function_symbol_table)
, symbols_(symbols ? symbols : managed_symbols_.get())
{
if(symbols == nullptr) {
symbols_ = new function_symbol_table;
}
std::vector<tk::token> tokens;
std::string::const_iterator i1 = text.begin(), i2 = text.end();
@ -213,13 +209,9 @@ formula::formula(const std::string& text, function_symbol_table* symbols)
formula::formula(const tk::token* i1, const tk::token* i2, function_symbol_table* symbols)
: expr_()
, str_()
, symbols_(symbols)
, managing_symbols(symbols == nullptr)
, managed_symbols_(symbols ? nullptr : new function_symbol_table)
, symbols_(symbols ? symbols : managed_symbols_.get())
{
if(symbols == nullptr) {
symbols_ = new function_symbol_table;
}
if(i1 != i2) {
expr_ = parse_expression(i1, i2, symbols);
} else {
@ -227,13 +219,6 @@ formula::formula(const tk::token* i1, const tk::token* i2, function_symbol_table
}
}
formula::~formula()
{
if(managing_symbols) {
delete symbols_;
}
}
formula_ptr formula::create_optional_formula(const std::string& str, function_symbol_table* symbols)
{
if(str.empty()) {

View file

@ -18,6 +18,7 @@
#include "formula/formula_fwd.hpp"
#include "formula/tokenizer.hpp"
#include "formula/variant.hpp"
#include <memory>
namespace wfl
{
@ -35,8 +36,6 @@ public:
formula(const std::string& str, function_symbol_table* symbols = nullptr);
formula(const tk::token* i1, const tk::token* i2, function_symbol_table* symbols = nullptr);
~formula();
static variant evaluate(
const const_formula_ptr& f,
const formula_callable& variables,
@ -80,8 +79,10 @@ private:
expression_ptr expr_;
std::string str_;
// Can't be a unique_ptr because function_symbol_table is an incomplete type,
// and the header it's declared in depends on this one.
const std::shared_ptr<function_symbol_table> managed_symbols_;
function_symbol_table* symbols_;
bool managing_symbols;
friend class formula_debugger;
};