libsdl-org / SDL

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

[SDL3] Video subsystem memory leak? #9794

Closed gresskar closed 5 months ago

gresskar commented 5 months ago

Some info:

I have a small skeleton program I compile with:

clang -std=c99 -fsanitize=address -I /usr/local/include -L /usr/local/lib -l SDL3 src/main.c -o bin/main:

#include <stdlib.h>
#include <SDL3/SDL.h>

int main(void)
{
    /* Initialize video subsystem */
    if (SDL_InitSubSystem(SDL_INIT_VIDEO) != 0)
    {
        SDL_Log("SDL_InitSubSystem(): %s", SDL_GetError());
        return EXIT_FAILURE;
    }

    /* Create a window */
    SDL_Window *window = SDL_CreateWindow("MyWindow", 800, 600, 0);

    if (window == NULL)
    {
        SDL_Log("SDL_CreateWindow(): %s", SDL_GetError());
        return EXIT_FAILURE;
    }

    /* Create a renderer */
    SDL_Renderer *renderer = SDL_CreateRenderer(window, NULL);

    if (renderer == NULL)
    {
        SDL_Log("SDL_CreateRenderer(): %s", SDL_GetError());
        return EXIT_FAILURE;
    }

    /* Clean up & exit */
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_QuitSubSystem(SDL_INIT_VIDEO);
    SDL_Quit();

    return EXIT_SUCCESS;
}

ASan output

=================================================================
==28783==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 2672 byte(s) in 1 object(s) allocated from:
    #0 0x590258339651 in calloc (/home/user/test/bin/main+0x11a651) (BuildId: fd93488bc90c155f90752249091ae1124e633d94)
    #1 0x74dde306776f  (<unknown module>)
    #2 0x74dde3067e4f  (<unknown module>)
    #3 0x74dde57068f4  (<unknown module>)

Direct leak of 920 byte(s) in 5 object(s) allocated from:
    #0 0x590258339651 in calloc (/home/user/test/bin/main+0x11a651) (BuildId: fd93488bc90c155f90752249091ae1124e633d94)
    #1 0x74dde59d2d8a  (<unknown module>)

Direct leak of 520 byte(s) in 13 object(s) allocated from:
    #0 0x5902583392e9 in malloc (/home/user/test/bin/main+0x11a2e9) (BuildId: fd93488bc90c155f90752249091ae1124e633d94)
    #1 0x74dde59e4248  (<unknown module>)

Direct leak of 96 byte(s) in 3 object(s) allocated from:
    #0 0x5902583392e9 in malloc (/home/user/test/bin/main+0x11a2e9) (BuildId: fd93488bc90c155f90752249091ae1124e633d94)
    #1 0x74dde59e0954  (<unknown module>)

Direct leak of 24 byte(s) in 1 object(s) allocated from:
    #0 0x5902583392e9 in malloc (/home/user/test/bin/main+0x11a2e9) (BuildId: fd93488bc90c155f90752249091ae1124e633d94)
    #1 0x74dde59e835f  (<unknown module>)

Indirect leak of 1005 byte(s) in 5 object(s) allocated from:
    #0 0x5902583383fa in realloc.part.0 asan_malloc_linux.cpp.o
    #1 0x74dde59e0a4f  (<unknown module>)

Indirect leak of 672 byte(s) in 2 object(s) allocated from:
    #0 0x5902583383fa in realloc.part.0 asan_malloc_linux.cpp.o
    #1 0x74dde59e18cc  (<unknown module>)

Indirect leak of 144 byte(s) in 1 object(s) allocated from:
    #0 0x590258339651 in calloc (/home/user/test/bin/main+0x11a651) (BuildId: fd93488bc90c155f90752249091ae1124e633d94)
    #1 0x74dde2ff178e  (<unknown module>)
    #2 0x447265646e65722e  (<unknown module>)

Indirect leak of 80 byte(s) in 2 object(s) allocated from:
    #0 0x5902583392e9 in malloc (/home/user/test/bin/main+0x11a2e9) (BuildId: fd93488bc90c155f90752249091ae1124e633d94)
    #1 0x74dde59e1288  (<unknown module>)

Indirect leak of 40 byte(s) in 1 object(s) allocated from:
    #0 0x590258339651 in calloc (/home/user/test/bin/main+0x11a651) (BuildId: fd93488bc90c155f90752249091ae1124e633d94)
    #1 0x74dde306725f  (<unknown module>)
    #2 0x74dde305c730  (<unknown module>)
    #3 0x74dde637ded1 in SDL_EGL_LoadLibrary (/usr/local/lib/libSDL3.so.0+0x17ded1) (BuildId: 68a5493af81362c1b6cf9994d2fe8823c64bf65e)
    #4 0x74dde6461147 in Wayland_GLES_LoadLibrary (/usr/local/lib/libSDL3.so.0+0x261147) (BuildId: 68a5493af81362c1b6cf9994d2fe8823c64bf65e)
    #5 0x74dde6397565 in SDL_GL_LoadLibrary_REAL (/usr/local/lib/libSDL3.so.0+0x197565) (BuildId: 68a5493af81362c1b6cf9994d2fe8823c64bf65e)
    #6 0x74dde6392f25 in SDL_RecreateWindow (/usr/local/lib/libSDL3.so.0+0x192f25) (BuildId: 68a5493af81362c1b6cf9994d2fe8823c64bf65e)
    #7 0x74dde62b369a in GL_CreateRenderer SDL_render_gl.c
    #8 0x74dde62a08aa in SDL_CreateRendererWithProperties_REAL (/usr/local/lib/libSDL3.so.0+0xa08aa) (BuildId: 68a5493af81362c1b6cf9994d2fe8823c64bf65e)
    #9 0x74dde62a0d33 in SDL_CreateRenderer_REAL (/usr/local/lib/libSDL3.so.0+0xa0d33) (BuildId: 68a5493af81362c1b6cf9994d2fe8823c64bf65e)
    #10 0x74dde625f468 in SDL_CreateRenderer (/usr/local/lib/libSDL3.so.0+0x5f468) (BuildId: 68a5493af81362c1b6cf9994d2fe8823c64bf65e)
    #11 0x590258381352 in main (/home/user/test/bin/main+0x162352) (BuildId: fd93488bc90c155f90752249091ae1124e633d94)
    #12 0x74dde600cc87  (/usr/lib/libc.so.6+0x25c87) (BuildId: 32a656aa5562eece8c59a585f5eacd6cf5e2307b)
    #13 0x74dde600cd4b in __libc_start_main (/usr/lib/libc.so.6+0x25d4b) (BuildId: 32a656aa5562eece8c59a585f5eacd6cf5e2307b)
    #14 0x59025824a104 in _start (/home/user/test/bin/main+0x2b104) (BuildId: fd93488bc90c155f90752249091ae1124e633d94)

Indirect leak of 24 byte(s) in 1 object(s) allocated from:
    #0 0x5902583392e9 in malloc (/home/user/test/bin/main+0x11a2e9) (BuildId: fd93488bc90c155f90752249091ae1124e633d94)
    #1 0x74dde59e835f  (<unknown module>)

Indirect leak of 24 byte(s) in 1 object(s) allocated from:
    #0 0x5902583392e9 in malloc (/home/user/test/bin/main+0x11a2e9) (BuildId: fd93488bc90c155f90752249091ae1124e633d94)
    #1 0x74dde59d887c  (<unknown module>)

SUMMARY: AddressSanitizer: 6221 byte(s) leaked in 36 allocation(s).
gresskar commented 5 months ago

There's still a memory leak when I cut out most code, but it's a couple KB smaller:

#include <stdlib.h>
#include <SDL3/SDL.h>

int main(void)
{
    /* Initialize video subsystem */
    if (SDL_InitSubSystem(SDL_INIT_VIDEO) != 0)
    {
        SDL_Log("SDL_InitSubSystem(): %s", SDL_GetError());
        return EXIT_FAILURE;
    }

    /* Clean up & exit */
    SDL_QuitSubSystem(SDL_INIT_VIDEO);
    SDL_Quit();

    return EXIT_SUCCESS;
}
=================================================================
==30384==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 736 byte(s) in 4 object(s) allocated from:
    #0 0x58e471109611 in calloc (/home/user/test/bin/main+0x11a611) (BuildId: 10777af3fb27884c534365126f854c135f64c827)
    #1 0x7ac3a87b2d8a  (<unknown module>)

Direct leak of 520 byte(s) in 13 object(s) allocated from:
    #0 0x58e4711092a9 in malloc (/home/user/test/bin/main+0x11a2a9) (BuildId: 10777af3fb27884c534365126f854c135f64c827)
    #1 0x7ac3a87c4248  (<unknown module>)

Direct leak of 184 byte(s) in 1 object(s) allocated from:
    #0 0x58e471109611 in calloc (/home/user/test/bin/main+0x11a611) (BuildId: 10777af3fb27884c534365126f854c135f64c827)
    #1 0x7ac3a87b2d8a  (<unknown module>)
    #2 0x2a00000021  (<unknown module>)

Direct leak of 96 byte(s) in 3 object(s) allocated from:
    #0 0x58e4711092a9 in malloc (/home/user/test/bin/main+0x11a2a9) (BuildId: 10777af3fb27884c534365126f854c135f64c827)
    #1 0x7ac3a87c0954  (<unknown module>)

Direct leak of 24 byte(s) in 1 object(s) allocated from:
    #0 0x58e4711092a9 in malloc (/home/user/test/bin/main+0x11a2a9) (BuildId: 10777af3fb27884c534365126f854c135f64c827)
    #1 0x7ac3a87c835f  (<unknown module>)

Indirect leak of 1184 byte(s) in 4 object(s) allocated from:
    #0 0x58e4711083ba in realloc.part.0 asan_malloc_linux.cpp.o
    #1 0x7ac3a87c18cc  (<unknown module>)

Indirect leak of 1060 byte(s) in 5 object(s) allocated from:
    #0 0x58e4711083ba in realloc.part.0 asan_malloc_linux.cpp.o
    #1 0x7ac3a87c0a4f  (<unknown module>)

Indirect leak of 40 byte(s) in 1 object(s) allocated from:
    #0 0x58e4711092a9 in malloc (/home/user/test/bin/main+0x11a2a9) (BuildId: 10777af3fb27884c534365126f854c135f64c827)
    #1 0x7ac3a87c1288  (<unknown module>)

Indirect leak of 24 byte(s) in 1 object(s) allocated from:
    #0 0x58e4711092a9 in malloc (/home/user/test/bin/main+0x11a2a9) (BuildId: 10777af3fb27884c534365126f854c135f64c827)
    #1 0x7ac3a87c835f  (<unknown module>)

Indirect leak of 24 byte(s) in 1 object(s) allocated from:
    #0 0x58e4711092a9 in malloc (/home/user/test/bin/main+0x11a2a9) (BuildId: 10777af3fb27884c534365126f854c135f64c827)
    #1 0x7ac3a87b887c  (<unknown module>)

SUMMARY: AddressSanitizer: 3892 byte(s) leaked in 34 allocation(s).
icculus commented 5 months ago

Generally, these are leaks in OpenGL's initialization code, not SDL.

icculus commented 5 months ago

(And D-bus.)