Skip to content

Commit

Permalink
fix memory leak risk of cpp target
Browse files Browse the repository at this point in the history
Signed-off-by: wangtao9 <[email protected]>
  • Loading branch information
wangtao9 committed Nov 18, 2023
1 parent d25d421 commit cefa47a
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ struct <lexer.name; format = "cap">StaticData final {

::antlr4::internal::OnceFlag <lexer.grammarName; format = "lower">LexerOnceFlag;
#if ANTLR4_USE_THREAD_LOCAL_CACHE
static thread_local
std::unique_ptr\<<lexer.name; format = "cap">StaticData> staticData = nullptr;

static thread_local
#endif
<lexer.name; format = "cap">StaticData *<lexer.grammarName; format = "lower">LexerStaticData = nullptr;
Expand All @@ -156,8 +159,10 @@ void <lexer.grammarName; format = "lower">LexerInitialize() {
}
#else
assert(<lexer.grammarName; format = "lower">LexerStaticData == nullptr);

auto
#endif
auto staticData = std::make_unique\<<lexer.name; format = "cap">StaticData>(
staticData = std::make_unique\<<lexer.name; format = "cap">StaticData>(
std::vector\<std::string>{
<lexer.ruleNames: {r | "<r>"}; separator = ", ", wrap, anchor>
},
Expand All @@ -175,7 +180,11 @@ void <lexer.grammarName; format = "lower">LexerInitialize() {
}
);
<atn>
#if ANTLR4_USE_THREAD_LOCAL_CACHE
<lexer.grammarName; format = "lower">LexerStaticData = staticData.get();
#else
<lexer.grammarName; format = "lower">LexerStaticData = staticData.release();
#endif
}

}
Expand Down Expand Up @@ -378,6 +387,9 @@ struct <parser.name; format = "cap">StaticData final {

::antlr4::internal::OnceFlag <parser.grammarName; format = "lower">ParserOnceFlag;
#if ANTLR4_USE_THREAD_LOCAL_CACHE
static thread_local
std::unique_ptr\<<parser.name; format = "cap">StaticData> staticData = nullptr;

static thread_local
#endif
<parser.name; format = "cap">StaticData *<parser.grammarName; format = "lower">ParserStaticData = nullptr;
Expand All @@ -389,8 +401,10 @@ void <parser.grammarName; format = "lower">ParserInitialize() {
}
#else
assert(<parser.grammarName; format = "lower">ParserStaticData == nullptr);

auto
#endif
auto staticData = std::make_unique\<<parser.name; format = "cap">StaticData>(
staticData = std::make_unique\<<parser.name; format = "cap">StaticData>(
std::vector\<std::string>{
<parser.ruleNames: {r | "<r>"}; separator = ", ", wrap, anchor>
},
Expand All @@ -402,7 +416,11 @@ void <parser.grammarName; format = "lower">ParserInitialize() {
}
);
<atn>
#if ANTLR4_USE_THREAD_LOCAL_CACHE
<parser.grammarName; format = "lower">ParserStaticData = staticData.get();
#else
<parser.grammarName; format = "lower">ParserStaticData = staticData.release();
#endif
}

}
Expand Down

0 comments on commit cefa47a

Please sign in to comment.