When running and compiling a simple equation more than once using ExprTK, I encounter a segmentation fault or address boundary error. Surprisingly, the issue disappears when I exclude the exprtk::collect_variables()
function, which doesn't make any sense since the function is just supposed to provide me a list with every variable in the equation.
#include "exprtk.h"
#include <iostream>
typedef exprtk::symbol_table<float> symbol_table_t;
typedef exprtk::expression<float> expression_t;
typedef exprtk::parser<float> parser_t;
parser_t parser;
expression_t expression;
symbol_table_t symbol_table;
float a = 5.0f;
std::string equation;
void f()
{
symbol_table.clear();
std::vector<std::string> varList;
exprtk::collect_variables(equation, varList); // DELETE THIS LINE FOR THE SEGFAULT TO GO AWAY
symbol_table.add_variable("a", a);
parser.compile(equation, expression);
std::cout << "value is " << expression.value() << std::endl;
}
int main()
{
equation.reserve(16);
equation = "a";
expression.register_symbol_table(symbol_table);
std::cout << "First call" << std::endl;
f();
std::cout << "Second call" << std::endl;
f();
return 0;
}
I really need the "collect_variables" function and I would appreciate if someone could tell me what is happening and how to I fix it.
Thanks in advance.
I tried to exclude the "symbol_table.clear()" call and the code works too, but it brings other issues.