A simple SDL2 program triggers a memory leak:
#include <SDL2/SDL.h>
#include <cstdio>
#include <memory>
#include <cstdlib>
int main() {
if (std::atexit(SDL_Quit)) {
printf("Registration failed\n");
return 1;
}
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL_Init error: %s", SDL_GetError());
std::exit(1);
}
auto free_window = [](SDL_Window* w) { SDL_DestroyWindow(w); };
std::unique_ptr<SDL_Window, decltype(free_window)> window{
SDL_CreateWindow("test window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 400, 400,
0),
std::move(free_window)};
if (!window) {
printf("SDL_CreateWindow error: %s", SDL_GetError());
std::exit(1);
}
auto free_surface = [](SDL_Surface* s) { SDL_FreeSurface(s); };
std::unique_ptr<SDL_Surface, decltype(free_surface)> surface{SDL_GetWindowSurface(window.get()),
std::move(free_surface)};
if (!surface) {
printf("SDL_GetWindowSurface error: %s", SDL_GetError());
std::exit(1);
}
SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
while (true) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
std::exit(0);
}
}
auto* pixels = reinterpret_cast<unsigned*>(surface->pixels);
for (int y = 0; y != 400; ++y) {
for (int x = 0; x != 400; ++x) {
pixels[y * 400 + x] = SDL_MapRGB(surface->format, 255, 255, 255);
}
}
SDL_UpdateWindowSurface(window.get());
}
}
Build:
g++ leak.cc -isystem /usr/include/SDL2 -fsanitize=address -o leak /usr/lib/libSDL2.so
ASAN output:
=================================================================
==46550==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 6280 byte(s) in 3 object(s) allocated from:
#0 0x7fd08a6bf411 in __interceptor_calloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:77
#1 0x7fd0858ba06f (/usr/lib/libnvidia-glcore.so.515.57+0xeba06f)
Direct leak of 1024 byte(s) in 1 object(s) allocated from:
#0 0x7fd08a6be7ea in __interceptor_realloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:85
#1 0x7fd0858b951a (/usr/lib/libnvidia-glcore.so.515.57+0xeb951a)
Direct leak of 520 byte(s) in 13 object(s) allocated from:
#0 0x7fd08a6bfa89 in __interceptor_malloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:69
#1 0x7fd08a0c2876 (<unknown module>)
Direct leak of 368 byte(s) in 2 object(s) allocated from:
#0 0x7fd08a6bf411 in __interceptor_calloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:77
#1 0x7fd08a0b11e0 (/usr/lib/libasan.so.8.0.0+0x7d11e0)
Direct leak of 112 byte(s) in 1 object(s) allocated from:
#0 0x7fd08a6bf411 in __interceptor_calloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:77
#1 0x7fd08a0be120 (<unknown module>)
Direct leak of 96 byte(s) in 3 object(s) allocated from:
#0 0x7fd08a6bfa89 in __interceptor_malloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:69
#1 0x7fd08a0beed5 (<unknown module>)
Direct leak of 40 byte(s) in 1 object(s) allocated from:
#0 0x7fd08a6bf411 in __interceptor_calloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:77
#1 0x7fd08a0bf348 (<unknown module>)
Direct leak of 24 byte(s) in 1 object(s) allocated from:
#0 0x7fd08a6bfa89 in __interceptor_malloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:69
#1 0x7fd08a0c6b00 (<unknown module>)
Direct leak of 8 byte(s) in 1 object(s) allocated from:
#0 0x7fd08a6be7ea in __interceptor_realloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:85
#1 0x7fd08a0b765a (/usr/lib/libasan.so.8.0.0+0x7d765a)
Indirect leak of 144448 byte(s) in 453 object(s) allocated from:
#0 0x7fd08a6bf411 in __interceptor_calloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:77
#1 0x7fd0858ba06f (/usr/lib/libnvidia-glcore.so.515.57+0xeba06f)
Indirect leak of 7989 byte(s) in 18 object(s) allocated from:
#0 0x7fd08a6be7ea in __interceptor_realloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:85
#1 0x7fd08a0befd5 (<unknown module>)
Indirect leak of 4930 byte(s) in 374 object(s) allocated from:
#0 0x7fd08a6bfa89 in __interceptor_malloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:69
#1 0x7fd0858b9bdc (/usr/lib/libnvidia-glcore.so.515.57+0xeb9bdc)
Indirect leak of 1596 byte(s) in 3 object(s) allocated from:
#0 0x7fd08a6bf411 in __interceptor_calloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:77
#1 0x7fd08a0bf595 (<unknown module>)
Indirect leak of 920 byte(s) in 5 object(s) allocated from:
#0 0x7fd08a6bf411 in __interceptor_calloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:77
#1 0x7fd08a0b11e0 (/usr/lib/libasan.so.8.0.0+0x7d11e0)
Indirect leak of 752 byte(s) in 6 object(s) allocated from:
#0 0x7fd08a6be7ea in __interceptor_realloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:85
#1 0x7fd0858b951a (/usr/lib/libnvidia-glcore.so.515.57+0xeb951a)
Indirect leak of 288 byte(s) in 4 object(s) allocated from:
#0 0x7fd08a6bfa89 in __interceptor_malloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:69
#1 0x7fd08a09bc12 (/usr/lib/libasan.so.8.0.0+0x7bbc12)
Indirect leak of 264 byte(s) in 1 object(s) allocated from:
#0 0x7fd08a6bf411 in __interceptor_calloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:77
#1 0x7fd08a0a18da (/usr/lib/libasan.so.8.0.0+0x7c18da)
Indirect leak of 240 byte(s) in 1 object(s) allocated from:
#0 0x7fd08a6bf411 in __interceptor_calloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:77
#1 0x7fd08a0c889c (<unknown module>)
#2 0x7ffffffff (<unknown module>)
Indirect leak of 232 byte(s) in 1 object(s) allocated from:
#0 0x7fd08a6bf411 in __interceptor_calloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:77
#1 0x7fd08a0bc8ee (<unknown module>)
Indirect leak of 184 byte(s) in 1 object(s) allocated from:
#0 0x7fd08a6bf411 in __interceptor_calloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:77
#1 0x7fd08a0b11e0 (/usr/lib/libasan.so.8.0.0+0x7d11e0)
#2 0xd00000004 (<unknown module>)
Indirect leak of 176 byte(s) in 2 object(s) allocated from:
#0 0x7fd08a6bf411 in __interceptor_calloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:77
#1 0x7fd08a0b6b70 (/usr/lib/libasan.so.8.0.0+0x7d6b70)
Indirect leak of 160 byte(s) in 4 object(s) allocated from:
#0 0x7fd08a6bfa89 in __interceptor_malloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:69
#1 0x7fd08a0c2876 (<unknown module>)
Indirect leak of 152 byte(s) in 7 object(s) allocated from:
#0 0x7fd08a6bfa89 in __interceptor_malloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:69
#1 0x7fd08a0bfc08 (<unknown module>)
Indirect leak of 128 byte(s) in 2 object(s) allocated from:
#0 0x7fd08a6bf411 in __interceptor_calloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:77
#1 0x7fd08a0b564d (/usr/lib/libasan.so.8.0.0+0x7d564d)
Indirect leak of 112 byte(s) in 1 object(s) allocated from:
#0 0x7fd08a6bf411 in __interceptor_calloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:77
#1 0x7fd08a0be120 (<unknown module>)
Indirect leak of 96 byte(s) in 1 object(s) allocated from:
#0 0x7fd08a6bf411 in __interceptor_calloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:77
#1 0x7fd08a0a9055 (/usr/lib/libasan.so.8.0.0+0x7c9055)
Indirect leak of 96 byte(s) in 2 object(s) allocated from:
#0 0x7fd08a6bfa89 in __interceptor_malloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:69
#1 0x7fd08a0b6c57 (/usr/lib/libasan.so.8.0.0+0x7d6c57)
Indirect leak of 80 byte(s) in 2 object(s) allocated from:
#0 0x7fd08a6bf411 in __interceptor_calloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:77
#1 0x7fd08a0bf348 (<unknown module>)
Indirect leak of 80 byte(s) in 2 object(s) allocated from:
#0 0x7fd08a6bfa89 in __interceptor_malloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:69
#1 0x7fd08a0b6bf2 (/usr/lib/libasan.so.8.0.0+0x7d6bf2)
Indirect leak of 64 byte(s) in 1 object(s) allocated from:
#0 0x7fd08a6be7ea in __interceptor_realloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:85
#1 0x7fd08a0a9141 (/usr/lib/libasan.so.8.0.0+0x7c9141)
Indirect leak of 64 byte(s) in 1 object(s) allocated from:
#0 0x7fd08a6bf411 in __interceptor_calloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:77
#1 0x7fd08a0c3b80 (<unknown module>)
Indirect leak of 48 byte(s) in 1 object(s) allocated from:
#0 0x7fd08a6bf411 in __interceptor_calloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:77
#1 0x7fd08a0a188a (/usr/lib/libasan.so.8.0.0+0x7c188a)
Indirect leak of 48 byte(s) in 1 object(s) allocated from:
#0 0x7fd08a6bf411 in __interceptor_calloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:77
#1 0x7fd08a0a18a1 (/usr/lib/libasan.so.8.0.0+0x7c18a1)
Indirect leak of 48 byte(s) in 2 object(s) allocated from:
#0 0x7fd08a6bfa89 in __interceptor_malloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:69
#1 0x7fd08a0c6b00 (<unknown module>)
Indirect leak of 38 byte(s) in 1 object(s) allocated from:
#0 0x7fd08a6bfa89 in __interceptor_malloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:69
#1 0x7fd08a0c002e (<unknown module>)
Indirect leak of 24 byte(s) in 1 object(s) allocated from:
#0 0x7fd08a6bf411 in __interceptor_calloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:77
#1 0x7fd08a0a1282 (/usr/lib/libasan.so.8.0.0+0x7c1282)
Indirect leak of 24 byte(s) in 1 object(s) allocated from:
#0 0x7fd08a6bf411 in __interceptor_calloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:77
#1 0x7fd08a0a1b4a (/usr/lib/libasan.so.8.0.0+0x7c1b4a)
Indirect leak of 24 byte(s) in 1 object(s) allocated from:
#0 0x7fd08a6bfa89 in __interceptor_malloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:69
#1 0x7fd08a0b6a5c (/usr/lib/libasan.so.8.0.0+0x7d6a5c)
Indirect leak of 16 byte(s) in 1 object(s) allocated from:
#0 0x7fd08a6be7ea in __interceptor_realloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:85
#1 0x7fd08a0c3eb3 (<unknown module>)
Indirect leak of 7 byte(s) in 1 object(s) allocated from:
#0 0x7fd08a6bfa89 in __interceptor_malloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:69
#1 0x7fd08a0beed5 (<unknown module>)
SUMMARY: AddressSanitizer: 171800 byte(s) leaked in 928 allocation(s).