antlr4 icon indicating copy to clipboard operation
antlr4 copied to clipboard

[Cpp] Fix cpp memory leaks

Open njnobles opened this issue 1 year ago • 1 comments

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 avatar May 15 '24 22:05 njnobles

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

njnobles avatar May 29 '24 16:05 njnobles

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

parrt avatar May 30 '24 18:05 parrt

Thanks, @mike-lischke !

parrt avatar May 31 '24 16:05 parrt

Thank you, @parrt and @mike-lischke!

njnobles avatar May 31 '24 16:05 njnobles