Memory leaks in monitor lexer (split from bug #1106)
Versatile Commodore Emulator
Brought to you by:
blackystardust,
gpz
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.
I have an old diff that fixes (most of) these monitor leaks, but there are two paths:
lib_free($2)s and similar in mon_parse.y, orI 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?
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 ;)
I could copypaste compyx answer - also tried 1) ... failed horribly :=)