Menu

#1936 Memory leaks in monitor lexer (split from bug #1106)

v3.x
open
compyx
Monitor
2023-09-17
2023-08-28
compyx
No

As stated in bug #1106 we have some nasty memory leaks in the lexer of the monitor.

When the monitor parses a command or a line of assembly the code generated from mon_lex.l calls lib_strdup() to set yylval.str, which is never freed properly. The same happens with on_add_name_to_symbol_table() and some other functions.
I've looked at this before and due to the auto-generated code, this bug is harder to fix than it looks unfortunately.

And this isn't one of those cases where "the OS will clean it up on exit" attitude can be excused: these leaks add up during runtime. For example: adding 4 labels results in 16 memory leaks, so imagine (re)loading a real label file or doing some extended debugging.

Discussion

  • Oskar Linde

    Oskar Linde - 2023-09-17

    I have an old diff that fixes (most of) these monitor leaks, but there are two paths:

    1. Manual adding a lot of lib_free($2)s and similar in mon_parse.y, or
    2. Making all the called function from mon_parse.y free their memory.

    I initially did (1) – free all yylval.str, but it felt a bit fragile as there were one or two instances where the called function actually needed to retain the memory pointer, and there's no documentation to help make sure this remains correct.

    The other option (2) seems like it would result in less lines of code and pushes the responsibility down to the receiver functions where any exception can be handled and documented locally.

    Any thoughts?

     
  • compyx

    compyx - 2023-09-17

    I definitely prefer the second option, I went down that first path too and it's a bit of rabbit hole.
    Although making the caller responsible for cleanup isn't great, we should be able to more easily identify where memory leaks occur, and plug them.

    Proper documentation would be required indeed, assuming anyone actually reads it ;)

     
  • gpz

    gpz - 2023-09-17

    I could copypaste compyx answer - also tried 1) ... failed horribly :=)

     

Log in to post a comment.

MongoDB Logo MongoDB