ReinhardPrix / FreedroidClassic

The original Freedroid, a free & open-source clone of the C64 classic 'Paradroid'.
GNU General Public License v2.0
39 stars 6 forks source link

memleak: main game #14

Closed matthiaskrgr closed 6 years ago

matthiaskrgr commented 6 years ago

Sorry, there was some miscommunication, when I meant that the other memleaks underlated to the editor still exist.

Maybe the ticket was not detailed enough.

These memleaks can be reproduced by just starting up the game and terminating it through the main menu:

Direct leak of 17894 byte(s) in 1 object(s) allocated from:
    #0 0x31226a in __interceptor_calloc /home/matthias/LLVM/LLVM6/stage_2/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:97:3
    #1 0x3a41ec in MyMalloc /tmp/FreedroidClassic/src/misc.c:1077:15
    #2 0x395866 in GetCrew /tmp/FreedroidClassic/src/map.c:1111:27
    #3 0x38a069 in main /tmp/FreedroidClassic/src/main.c:86:7
Direct leak of 17015 byte(s) in 1 object(s) allocated from:
    #0 0x31226a in __interceptor_calloc /home/matthias/LLVM/LLVM6/stage_2/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:97:3
    #1 0x3a41ec in MyMalloc /tmp/FreedroidClassic/src/misc.c:1077:15
    #2 0x3944b5 in GetLiftConnections /tmp/FreedroidClassic/src/map.c:986:10
    #3 0x3755c1 in InitNewMission /tmp/FreedroidClassic/src/init.c:707:7
    #4 0x38a069 in main /tmp/FreedroidClassic/src/main.c:86:7
Direct leak of 901 byte(s) in 1 object(s) allocated from:
    #0 0x31226a in __interceptor_calloc /home/matthias/LLVM/LLVM6/stage_2/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:97:3
    #1 0x3a41ec in MyMalloc /tmp/FreedroidClassic/src/misc.c:1077:15
    #2 0x375b16 in InitNewMission /tmp/FreedroidClassic/src/init.c:810:3
    #3 0x38a069 in main /tmp/FreedroidClassic/src/main.c:86:7
ReinhardPrix commented 6 years ago

Thanks a lot for pointing this out. Indeed, this game (coming from the stone age) didn't really bother with freeing any memory at exit, and left this for the OS. This isn't a big issue, but I thought it's indeed better to remove those, it's cleaner and should help finding actual leaks and other problems.

@matthiaskrgr btw, what tool are you using for finding memory leaks? I'm running valgrind, but that also produces lots of spurious stuff, such a leaks in the system library and SDL.

matthiaskrgr commented 6 years ago

I'm using the address sanitizer https://clang.llvm.org/docs/AddressSanitizer.html#id7 just build with -fsanitize=address cflag and run the game. It will show the leaks on exit.

ReinhardPrix commented 6 years ago

that's awesome, thanks for the hint, looks like a very nice tool.

matthiaskrgr commented 6 years ago

Yes, it already helped me found tens of bugs in various programs/games :) Note that you can also combine address and undefined behaviour sanitizer -fsanitize=address,undefined (UBSan is how I found the zeroDiv in https://github.com/ReinhardPrix/FreedroidClassic/issues/8 )

-g3 -Og -fsanitize=address,undefined -fno-omit-frame-pointer -fno-optimize-sibling-calls is usually a good setup to detect such bugs (run with export UBSAN_OPTIONS=print_stacktrace=1 for ubsan-runtime backtraces)

matthiaskrgr commented 6 years ago

You can get more info on the sanitizer runtime configurations with ASAN_OPTIONS=help=1 ./freedroid and UBSAN_OPTIONS=help=1 ./freedroid respectively.