libsdl-org / SDL

Simple Directmedia Layer
https://libsdl.org
zlib License
10.22k stars 1.86k forks source link

testautomation: pen tests leak memory #9279

Closed madebr closed 8 months ago

madebr commented 8 months ago

Running testautomation --filter pen --trackmem shows leaked memory.

/cc @creichen

The leaks ``` INFO: Memory allocations: Allocation 0: 64 bytes 0x7f92116f682d: SDL_calloc_REAL+0x45 0x7f921165c43b: SDL_calloc+0x27 0x41cbd8: SDL_SUT_PenModifyBegin+0x1e6 0x41f635: _pen_register+0x2e 0x41f7b5: pen_iteration+0x135 0x44b132: SDLTest_RunTest+0x132 0x44bb0b: SDLTest_RunSuites+0x842 0x408e8a: main+0x4e7 0x7f9211449b8a: __libc_start_call_main+0x7a 0x7f9211449c4b: __libc_start_main+0x8b 0x4088b5: _start+0x25 Allocation 1: 2184 bytes 0x7f92116f689b: SDL_realloc_REAL+0x3d 0x7f921165d024: SDL_realloc+0x27 0x41cacb: SDL_SUT_PenModifyBegin+0xd9 0x41f635: _pen_register+0x2e 0x423685: pen_mouseEmulationDelayed+0x69 0x44b132: SDLTest_RunTest+0x132 0x44bb0b: SDLTest_RunSuites+0x842 0x408e8a: main+0x4e7 0x7f9211449b8a: __libc_start_call_main+0x7a 0x7f9211449c4b: __libc_start_main+0x8b 0x4088b5: _start+0x25 Allocation 2: 64 bytes 0x7f92116f682d: SDL_calloc_REAL+0x45 0x7f921165c43b: SDL_calloc+0x27 0x41cbd8: SDL_SUT_PenModifyBegin+0x1e6 0x41f635: _pen_register+0x2e 0x420581: pen_buttonReporting+0xd9 0x44b132: SDLTest_RunTest+0x132 0x44bb0b: SDLTest_RunSuites+0x842 0x408e8a: main+0x4e7 0x7f9211449b8a: __libc_start_call_main+0x7a 0x7f9211449c4b: __libc_start_main+0x8b 0x4088b5: _start+0x25 Allocation 3: 64 bytes 0x7f92116f682d: SDL_calloc_REAL+0x45 0x7f921165c43b: SDL_calloc+0x27 0x41cbd8: SDL_SUT_PenModifyBegin+0x1e6 0x422c04: pen_initAndInfo+0x22e 0x44b132: SDLTest_RunTest+0x132 0x44bb0b: SDLTest_RunSuites+0x842 0x408e8a: main+0x4e7 0x7f9211449b8a: __libc_start_call_main+0x7a 0x7f9211449c4b: __libc_start_main+0x8b 0x4088b5: _start+0x25 Allocation 4: 64 bytes 0x7f92116f682d: SDL_calloc_REAL+0x45 0x7f921165c43b: SDL_calloc+0x27 0x41cbd8: SDL_SUT_PenModifyBegin+0x1e6 0x41f635: _pen_register+0x2e 0x420359: pen_GUIDs+0xa2 0x44b132: SDLTest_RunTest+0x132 0x44bb0b: SDLTest_RunSuites+0x842 0x408e8a: main+0x4e7 0x7f9211449b8a: __libc_start_call_main+0x7a 0x7f9211449c4b: __libc_start_main+0x8b 0x4088b5: _start+0x25 Allocation 5: 64 bytes 0x7f92116f682d: SDL_calloc_REAL+0x45 0x7f921165c43b: SDL_calloc+0x27 0x41cbd8: SDL_SUT_PenModifyBegin+0x1e6 0x422d6c: pen_initAndInfo+0x396 0x44b132: SDLTest_RunTest+0x132 0x44bb0b: SDLTest_RunSuites+0x842 0x408e8a: main+0x4e7 0x7f9211449b8a: __libc_start_call_main+0x7a 0x7f9211449c4b: __libc_start_main+0x8b 0x4088b5: _start+0x25 Allocation 6: 64 bytes 0x7f92116f682d: SDL_calloc_REAL+0x45 0x7f921165c43b: SDL_calloc+0x27 0x41cbd8: SDL_SUT_PenModifyBegin+0x1e6 0x41f635: _pen_register+0x2e 0x41fc95: pen_hotplugging+0x82 0x44b132: SDLTest_RunTest+0x132 0x44bb0b: SDLTest_RunSuites+0x842 0x408e8a: main+0x4e7 0x7f9211449b8a: __libc_start_call_main+0x7a 0x7f9211449c4b: __libc_start_main+0x8b 0x4088b5: _start+0x25 Allocation 7: 64 bytes 0x7f92116f682d: SDL_calloc_REAL+0x45 0x7f921165c43b: SDL_calloc+0x27 0x41cbd8: SDL_SUT_PenModifyBegin+0x1e6 0x422e14: pen_initAndInfo+0x43e 0x44b132: SDLTest_RunTest+0x132 0x44bb0b: SDLTest_RunSuites+0x842 0x408e8a: main+0x4e7 0x7f9211449b8a: __libc_start_call_main+0x7a 0x7f9211449c4b: __libc_start_main+0x8b 0x4088b5: _start+0x25 Allocation 8: 64 bytes 0x7f92116f682d: SDL_calloc_REAL+0x45 0x7f921165c43b: SDL_calloc+0x27 0x41cbd8: SDL_SUT_PenModifyBegin+0x1e6 0x41f635: _pen_register+0x2e 0x4220cb: pen_movementAndAxes+0xec7 0x44b132: SDLTest_RunTest+0x132 0x44bb0b: SDLTest_RunSuites+0x842 0x408e8a: main+0x4e7 0x7f9211449b8a: __libc_start_call_main+0x7a 0x7f9211449c4b: __libc_start_main+0x8b 0x4088b5: _start+0x25 Allocation 9: 64 bytes 0x7f92116f682d: SDL_calloc_REAL+0x45 0x7f921165c43b: SDL_calloc+0x27 0x41cbd8: SDL_SUT_PenModifyBegin+0x1e6 0x41f635: _pen_register+0x2e 0x423685: pen_mouseEmulationDelayed+0x69 0x44b132: SDLTest_RunTest+0x132 0x44bb0b: SDLTest_RunSuites+0x842 0x408e8a: main+0x4e7 0x7f9211449b8a: __libc_start_call_main+0x7a 0x7f9211449c4b: __libc_start_main+0x8b 0x4088b5: _start+0x25 Allocation 10: 64 bytes 0x7f92116f682d: SDL_calloc_REAL+0x45 0x7f921165c43b: SDL_calloc+0x27 0x41cbd8: SDL_SUT_PenModifyBegin+0x1e6 0x41f635: _pen_register+0x2e 0x420359: pen_GUIDs+0xa2 0x44b132: SDLTest_RunTest+0x132 0x44bb0b: SDLTest_RunSuites+0x842 0x408e8a: main+0x4e7 0x7f9211449b8a: __libc_start_call_main+0x7a 0x7f9211449c4b: __libc_start_main+0x8b 0x4088b5: _start+0x25 Allocation 11: 64 bytes 0x7f92116f682d: SDL_calloc_REAL+0x45 0x7f921165c43b: SDL_calloc+0x27 0x41cbd8: SDL_SUT_PenModifyBegin+0x1e6 0x41f635: _pen_register+0x2e 0x420549: pen_buttonReporting+0xa1 0x44b132: SDLTest_RunTest+0x132 0x44bb0b: SDLTest_RunSuites+0x842 0x408e8a: main+0x4e7 0x7f9211449b8a: __libc_start_call_main+0x7a 0x7f9211449c4b: __libc_start_main+0x8b 0x4088b5: _start+0x25 Allocation 12: 64 bytes 0x7f92116f682d: SDL_calloc_REAL+0x45 0x7f921165c43b: SDL_calloc+0x27 0x41cbd8: SDL_SUT_PenModifyBegin+0x1e6 0x41f635: _pen_register+0x2e 0x41f7e8: pen_iteration+0x168 0x44b132: SDLTest_RunTest+0x132 0x44bb0b: SDLTest_RunSuites+0x842 0x408e8a: main+0x4e7 0x7f9211449b8a: __libc_start_call_main+0x7a 0x7f9211449c4b: __libc_start_main+0x8b 0x4088b5: _start+0x25 Allocation 13: 64 bytes 0x7f92116f682d: SDL_calloc_REAL+0x45 0x7f921165c43b: SDL_calloc+0x27 0x41cbd8: SDL_SUT_PenModifyBegin+0x1e6 0x41f635: _pen_register+0x2e 0x4231c5: pen_mouseEmulation+0x69 0x44b132: SDLTest_RunTest+0x132 0x44bb0b: SDLTest_RunSuites+0x842 0x408e8a: main+0x4e7 0x7f9211449b8a: __libc_start_call_main+0x7a 0x7f9211449c4b: __libc_start_main+0x8b 0x4088b5: _start+0x25 Allocation 14: 64 bytes 0x7f92116f682d: SDL_calloc_REAL+0x45 0x7f921165c43b: SDL_calloc+0x27 0x41cbd8: SDL_SUT_PenModifyBegin+0x1e6 0x41f635: _pen_register+0x2e 0x41f780: pen_iteration+0x100 0x44b132: SDLTest_RunTest+0x132 0x44bb0b: SDLTest_RunSuites+0x842 0x408e8a: main+0x4e7 0x7f9211449b8a: __libc_start_call_main+0x7a 0x7f9211449c4b: __libc_start_main+0x8b 0x4088b5: _start+0x25 Allocation 15: 64 bytes 0x7f92116f682d: SDL_calloc_REAL+0x45 0x7f921165c43b: SDL_calloc+0x27 0x41cbd8: SDL_SUT_PenModifyBegin+0x1e6 0x41f635: _pen_register+0x2e 0x420359: pen_GUIDs+0xa2 0x44b132: SDLTest_RunTest+0x132 0x44bb0b: SDLTest_RunSuites+0x842 0x408e8a: main+0x4e7 0x7f9211449b8a: __libc_start_call_main+0x7a 0x7f9211449c4b: __libc_start_main+0x8b 0x4088b5: _start+0x25 Allocation 16: 64 bytes 0x7f92116f682d: SDL_calloc_REAL+0x45 0x7f921165c43b: SDL_calloc+0x27 0x41cbd8: SDL_SUT_PenModifyBegin+0x1e6 0x422a2a: pen_initAndInfo+0x54 0x44b132: SDLTest_RunTest+0x132 0x44bb0b: SDLTest_RunSuites+0x842 0x408e8a: main+0x4e7 0x7f9211449b8a: __libc_start_call_main+0x7a 0x7f9211449c4b: __libc_start_main+0x8b 0x4088b5: _start+0x25 Allocation 17: 64 bytes 0x7f92116f682d: SDL_calloc_REAL+0x45 0x7f921165c43b: SDL_calloc+0x27 0x41cbd8: SDL_SUT_PenModifyBegin+0x1e6 0x41f635: _pen_register+0x2e 0x420359: pen_GUIDs+0xa2 0x44b132: SDLTest_RunTest+0x132 0x44bb0b: SDLTest_RunSuites+0x842 0x408e8a: main+0x4e7 0x7f9211449b8a: __libc_start_call_main+0x7a 0x7f9211449c4b: __libc_start_main+0x8b 0x4088b5: _start+0x25 Allocation 18: 64 bytes 0x7f92116f682d: SDL_calloc_REAL+0x45 0x7f921165c43b: SDL_calloc+0x27 0x41cbd8: SDL_SUT_PenModifyBegin+0x1e6 0x41f635: _pen_register+0x2e 0x41fc68: pen_hotplugging+0x55 0x44b132: SDLTest_RunTest+0x132 0x44bb0b: SDLTest_RunSuites+0x842 0x408e8a: main+0x4e7 0x7f9211449b8a: __libc_start_call_main+0x7a 0x7f9211449c4b: __libc_start_main+0x8b 0x4088b5: _start+0x25 Allocation 19: 64 bytes 0x7f92116f682d: SDL_calloc_REAL+0x45 0x7f921165c43b: SDL_calloc+0x27 0x41cbd8: SDL_SUT_PenModifyBegin+0x1e6 0x422fa3: pen_initAndInfo+0x5cd 0x44b132: SDLTest_RunTest+0x132 0x44bb0b: SDLTest_RunSuites+0x842 0x408e8a: main+0x4e7 0x7f9211449b8a: __libc_start_call_main+0x7a 0x7f9211449c4b: __libc_start_main+0x8b 0x4088b5: _start+0x25 Allocation 20: 64 bytes 0x7f92116f682d: SDL_calloc_REAL+0x45 0x7f921165c43b: SDL_calloc+0x27 0x41cbd8: SDL_SUT_PenModifyBegin+0x1e6 0x41f635: _pen_register+0x2e 0x41fd85: pen_hotplugging+0x172 0x44b132: SDLTest_RunTest+0x132 0x44bb0b: SDLTest_RunSuites+0x842 0x408e8a: main+0x4e7 0x7f9211449b8a: __libc_start_call_main+0x7a 0x7f9211449c4b: __libc_start_main+0x8b 0x4088b5: _start+0x25 Allocation 21: 64 bytes 0x7f92116f682d: SDL_calloc_REAL+0x45 0x7f921165c43b: SDL_calloc+0x27 0x41cbd8: SDL_SUT_PenModifyBegin+0x1e6 0x422b03: pen_initAndInfo+0x12d 0x44b132: SDLTest_RunTest+0x132 0x44bb0b: SDLTest_RunSuites+0x842 0x408e8a: main+0x4e7 0x7f9211449b8a: __libc_start_call_main+0x7a 0x7f9211449c4b: __libc_start_main+0x8b 0x4088b5: _start+0x25 Allocation 22: 64 bytes 0x7f92116f682d: SDL_calloc_REAL+0x45 0x7f921165c43b: SDL_calloc+0x27 0x41cbd8: SDL_SUT_PenModifyBegin+0x1e6 0x41f635: _pen_register+0x2e 0x422098: pen_movementAndAxes+0xe94 0x44b132: SDLTest_RunTest+0x132 0x44bb0b: SDLTest_RunSuites+0x842 0x408e8a: main+0x4e7 0x7f9211449b8a: __libc_start_call_main+0x7a 0x7f9211449c4b: __libc_start_main+0x8b 0x4088b5: _start+0x25 Total: 3.51 Kb in 23 allocations ```
creichen commented 8 months ago

Thank you for the report! This looks like SDL_pen.c, line 312:

        pen->name = (char *)SDL_calloc(1, SDL_PEN_MAX_NAME); /* Never deallocated */

Correction: Earlier I wrote that there is no SDL_PenQuit(), which was incorrect. I have updated the text below to reflect reality.

This is memory that would only ever be deallocated during SDL_PenQuit(), except that SDL_PenQuit() only deallocates hint callbacks right now, not the registry of currently or previously attached pens.

While I doubt that this is or will become practically meaningful in production, it would keep tripping up memory leak scans. I will put together a PR for it.

Background: The pen subsystem tracks pens throughout the lifetime of the program, to make sure that pens that get detached and reattached retain the same and to allow the client code to keep a pointer to the pen name to display e.g. in a configuration menu. The only safe time to deallocate is thus when the program shuts down, or when the client code explicitly signals that it wants to shut down SDL, at which point it is reasonable to assume that the client won't expect SDL-managed pointers to still be valid.

creichen commented 8 months ago

One of the reported leaks was indicative of a real leak (cf. #9341). Thank you again for the report!

madebr commented 8 months ago

Thanks for the fix :)