I am trying to create AST with semantic rules while parsing with boost::spirit. AST must be built only for piece of the input, another part of the input should be parsed without sintax tree.
For example, for such input strings: "self.usedFoo(Bar).filter(self.baz > baz)" or "self.Foo.filter(true)" AST should be build only for bold part.
And there is a problem: parser runs multimple times parsing grammar and calling semantic action (instatntiating AST nodes) multimple times too, so I got terrible memory leaks.
Simplicated source code:
grammar:
line = stmt | stmt >> "filter.(" >> filter >> ')';
filter %= (filterterm)
filterterm %= (filterfactor)
filterfactor = value [phoenix::bind(&ValueFilterSemanticNode::Instantiate, qi::_val, qi::_1)];
Instantiating node:
static void ValueFilterSemanticNode::Instantiate(QVariant &res, QVariant &value)
{
qDebug() << " Creating new Value Node...";
ValueFilterSemanticNode *n = new ValueFilterSemanticNode();
qDebug() << " " << n;
n->value = QVariant(value.toInt());
res = QVariant::fromValue(n);
}
input:
self.filter(1)
debug out:
Creating new Value Node...
0x22fdfd0
Creating new Value Node...
0x22fe030
Creating new Value Node...
0x22fde50
[...many many lines...]
Creating new Value Node...
0x22fe238
Creating new Value Node...
0x22fe218
Running Filter test
Value node running... 0x22fe218
Check result = QVariant(int, 1)
So, as you can see, nodes instantiating too many times that causes mem leaks.
See Question&Answers more detail:os