Hi Mike,
First of all: thank you so much for the cpp target of antlr4!
I discovered a memleak caused by a (hidden) circular reference:
The RecognitionException has a shared_ptr to the ParserRuleContext, but is stored in the ParserRuleContext, wrapped in the std::exception_ptr.
This happens in the generated lines in the XXParser in the catch section of the rule methods:
_localctx->exception = std::current_exception();
I am under the impression that this leads quite generally to a memleak, but I would be happy to create a small reproducing example if you want.
We are bailing on whatever error, so I have created a workaround by implementing a new BailErrorStrategy that sets the member of the Context to nullptr.
...
virtual void recover(antlr4::Parser *recognizer, std::exception_ptr e) override
{
auto ctx = recognizer->getContext();
ctx->exception = nullptr;
throw antlr4::ParseCancellationException();
}
..
I would say that having the reference to the exception in the context is not necessary, as it is already provided in the report and recover methods of the errorhandler, but removing the reference would of course be a breaking change.
Kind regards,
Floor Goddijn