-
Couldn't load subscription status.
- Fork 180
Description
I found an odd memory leak. It was reported by ASAN locally, where I use sdl2-compat, but it did not trigger in CI, where we use regular sdl2 from ubuntu-latest.
SDL_FreeSurface() does not free the pixel memory for surfaces returned by TTF_RenderUTF8_Blended() if the color's alpha component is 0xff. The problem disappears when running SDL_SetSurfaceBlendMode() on the created surface.
I am not sure if the bug is in SDL2_ttf or sdl2-compat, however SDL2_ttf directly accesses members of the surface struct, so opening the issue here.
I am using the fully up-to-date Arch Linux packages of SDL2_ttf, sdl2-compat and sdl3 and installed the debug symbols for SDL2_ttf so you can see the stack trace below.
Repro:
// main.c
#include <assert.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
int main(int argc, char** argv)
{
int init = TTF_Init();
assert(init == 0);
const char* fontFile = "/usr/share/fonts/droid/DroidSansFallback.ttf";
TTF_Font *font = TTF_OpenFont(fontFile, 11);
assert(font);
SDL_Color color = {0, 0, 0, 0xff};
const char* text = "Hello World!";
SDL_Surface *surf = TTF_RenderUTF8_Blended(font, text, color);
// SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_BLEND);
// ^ Problem goes away if you uncomment this line
SDL_FreeSurface(surf);
TTF_CloseFont(font);
TTF_Quit();
}gcc -fsanitize=address -lSDL2 -lSDL2_ttf main.c && ./a.out # or
# clang -fsanitize=address -lSDL2 -lSDL2_ttf main.c && ./a.out # also reports the same problem==1062420==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 4583 byte(s) in 1 object(s) allocated from:
#0 0x7f31b4920e15 in malloc /usr/src/debug/gcc/gcc/libsanitizer/asan/asan_malloc_linux.cpp:67
#1 0x7b31b0ace82d (/usr/lib/../lib/libSDL3.so.0+0xce82d) (BuildId: d5d9a839369f6d4021f6c1c8ebd0cc02db83050a)
#2 0x7f31b4fe345e in AllocateAlignedPixels /usr/src/debug/sdl2_ttf/SDL2_ttf-2.24.0/SDL_ttf.c:1462
#3 0x7f31b4fe3a73 in Create_Surface_Blended /usr/src/debug/sdl2_ttf/SDL2_ttf-2.24.0/SDL_ttf.c:1584
#4 0x7f31b4fee94d in TTF_Render_Internal /usr/src/debug/sdl2_ttf/SDL2_ttf-2.24.0/SDL_ttf.c:3546
#5 0x55dbd5897336 in main (/tmp/sdl2-ttf-leak/a.out+0x1336) (BuildId: 2b4439da1b54dbef64e2d78b0f39764c22d6a936)
#6 0x7f31b46376b4 (/usr/lib/libc.so.6+0x276b4) (BuildId: 468e3585c794491a48ea75fceb9e4d6b1464fc35)
#7 0x7f31b4637768 in __libc_start_main (/usr/lib/libc.so.6+0x27768) (BuildId: 468e3585c794491a48ea75fceb9e4d6b1464fc35)
#8 0x55dbd5897124 in _start (/tmp/sdl2-ttf-leak/a.out+0x1124) (BuildId: 2b4439da1b54dbef64e2d78b0f39764c22d6a936)
SUMMARY: AddressSanitizer: 4583 byte(s) leaked in 1 allocation(s).
Also found by valgrind:
$ gcc -lSDL2 -lSDL2_ttf main.c && valgrind ./a.out 2>&1 | grep definitely
==1201625== definitely lost: 4,583 bytes in 1 blocks
Here are the installed packages:
$ pacman -Q | grep '^sdl[2-3]'
sdl2-compat 2.32.56-1
sdl2_image 2.8.8-1
sdl2_mixer 2.8.1-2
sdl2_net 1:2.2.0-2
sdl2_ttf 2.24.0-1
sdl2_ttf-debug 2.24.0-1
sdl3 3.2.16-1