I’m writing a very basic SDL application, but when I run it through valgrind, it reports several lost blocks apparently related to the SDL library (before you ask it, I do call SDL_Quit, or more precisely I call atexit(SDL_Quit)). Here is an example:
==2525== 192 (16 direct, 176 indirect) bytes in 1 blocks are definitely lost in loss record 107 of 131
==2525== at 0x4C25502: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2525== by 0x644244A: ??? (in /usr/lib/libX11.so.6.3.0)
==2525== by 0x6442989: ??? (in /usr/lib/libX11.so.6.3.0)
==2525== by 0x64440A2: ??? (in /usr/lib/libX11.so.6.3.0)
==2525== by 0x6444915: _XlcCreateLC (in /usr/lib/libX11.so.6.3.0)
==2525== by 0x6462B5F: _XlcDefaultLoader (in /usr/lib/libX11.so.6.3.0)
==2525== by 0x644C325: _XOpenLC (in /usr/lib/libX11.so.6.3.0)
==2525== by 0x644C467: _XlcCurrentLC (in /usr/lib/libX11.so.6.3.0)
==2525== by 0x644C4BD: XSetLocaleModifiers (in /usr/lib/libX11.so.6.3.0)
==2525== by 0x4E69EED: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==2525== by 0x4E6A57F: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==2525== by 0x4E59E00: SDL_VideoInit (in /usr/lib/libSDL-1.2.so.0.11.3)
I searched here on StackOverflow and found a similar question. The answer was that apparently, there are some small buffers allocated within the library which the writers never care to free. My question is, what is the motivation of such an approach? Why allocate something and then deliberately not free() it?
If the buffers are needed till the end of the program there is no need to free them. When a program exits all memory it was using is freed by the OS anyway. If the program bothered to free the blocks before exiting it would only serve to slow down the exiting of the program.