Skip to content

Commit

Permalink
[clang-repl] Remove memory leak of ASTContext/TargetMachine.
Browse files Browse the repository at this point in the history
Removes memory leak of ASTContext and TargetMachine. When DisableFree is turned on, it intentionally leaks these instances as they can be trivially deallocated. This patch turns this off and delete Parser instance early so that they will not reference dangling pargma headers.

Asan shouldn't detect these as leaks normally, since burypointer is called for them. But, every invocation of incremental parser createa an additional leak of TargetMachine. If there are many invocations within a single test case, we easily reach number of leaks exceeding kGraveYardMaxSize (which is 12) and leaks start to get reported by asan buildbots.

Reviewed By: v.g.vassilev

Differential Revision: https://reviews.llvm.org/D127991
  • Loading branch information
sunho committed Jun 17, 2022
1 parent 8da8b61 commit 7bc00ce
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
5 changes: 4 additions & 1 deletion clang/lib/Interpreter/IncrementalParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ IncrementalParser::IncrementalParser(std::unique_ptr<CompilerInstance> Instance,
P->Initialize();
}

IncrementalParser::~IncrementalParser() { Act->FinalizeAction(); }
IncrementalParser::~IncrementalParser() {
P.reset();
Act->FinalizeAction();
}

llvm::Expected<PartialTranslationUnit &>
IncrementalParser::ParseOrWrapTopLevelDecl() {
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Interpreter/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ CreateCI(const llvm::opt::ArgStringList &Argv) {
// times, reusing the same AST.
Clang->getCodeGenOpts().ClearASTBeforeBackend = false;

Clang->getFrontendOpts().DisableFree = false;
Clang->getCodeGenOpts().DisableFree = false;

return std::move(Clang);
}

Expand Down

0 comments on commit 7bc00ce

Please sign in to comment.