antlr / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
http://antlr.org
BSD 3-Clause "New" or "Revised" License
17.04k stars 3.27k forks source link

[Cpp] Fix cpp memory leaks #4617

Closed njnobles closed 4 months ago

njnobles commented 4 months ago

This PR fixes memory leaks caused by the StaticData objects associated with the generated Parser and Lexer classes.

Found using the CRT library in VS2022. With the demo project, it produces a leak report similar to this:

Detected memory leaks!
Dumping objects ->
{21126} normal block at 0x000001AAB1D11180, 32 bytes long.
 Data: <                > 00 95 CF B1 AA 01 00 00 A0 9A CF B1 AA 01 00 00 
{20999} normal block at 0x000001AAB1D141E0, 32 bytes long.
 Data: <`       `       > 60 1F D1 B1 AA 01 00 00 60 1F D1 B1 AA 01 00 00 
{20982} normal block at 0x000001AAB1D134C0, 32 bytes long.
 Data: <         1      > C0 A8 CD B1 AA 01 00 00 C0 31 D1 B1 AA 01 00 00 
{20981} normal block at 0x000001AAB1D20DF0, 128 bytes long.
 Data: <                > 00 19 D1 B1 AA 01 00 00 00 19 D1 B1 AA 01 00 00 
...<~7000 lines long>

Valgrind on Ubuntu produces a similarly large leak detection.

Switching the StaticData objects to be unique_ptr instead of raw pointers removes all but 1 leak. The remaining leak is the ATNDeserializationOptions which is also fixed by a unique_ptr.

njnobles commented 4 months ago

Hi @parrt, would it be possible to get someone to review this PR? Thank you!

parrt commented 4 months ago

Unfortunately I'm not qualified to evaluate this :( Maybe @mike-lischke ? @jcking ?

parrt commented 4 months ago

Thanks, @mike-lischke !

njnobles commented 4 months ago

Thank you, @parrt and @mike-lischke!