C++ target , memory leak / crash

26 views
Skip to first unread message

Koen Samyn

unread,
Mar 8, 2017, 10:42:59 AM3/8/17
to antlr-discussion
Some problems with the C++ target:

  1. Following code crashes with the deletion of the parse tree
  2. If I use Ref with the parse tree then the program also crashes when the shared_ptr deletes the ptr.
  3. Not deleting and not using the Ref obviously leads to memory leaks.

Anybody know how to fix this?

void parse(std::ifstream& stream) {
if (stream.is_open())
{
ANTLRInputStream input(stream);
textworldLexer lexer(&input);
CommonTokenStream tokens(&lexer);
textworldParser parser(&tokens);
tree::ParseTree* tree = parser.world();

Ref<textworldVisitor> visitor(new MyVisitor());
visitor->visit(tree);

delete tree;

getchar();
}
}

Mike Lischke

unread,
Mar 8, 2017, 10:53:36 AM3/8/17
to antlr-di...@googlegroups.com
Hi Koen,


  1. Following code crashes with the deletion of the parse tree
  2. If I use Ref with the parse tree then the program also crashes when the shared_ptr deletes the ptr.
  3. Not deleting and not using the Ref obviously leads to memory leaks.
I wrote an own paragraph in the C++ target description about that: https://github.com/antlr/antlr4/blob/master/doc/cpp-target.md#memory-management.

General rule: don't try to manage raw pointers from the runtime, they are all managed internally.


Reply all
Reply to author
Forward
0 new messages