AddressSanitizer:在一次分配中泄漏了128字节,发生在SDL_CreateWindowAndRenderer
Context
我在Linux的 Fedora 43上
我正在用 SDL3.4.8(但我也试过 SDL 3.4.2,问题还是一样)
Code
#include <SDL3/SDL.h>
int main() {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_CreateWindowAndRenderer("Leak Test", 640, 480, 0, &window, &renderer);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Memory Leak trace
在运行时,我发现了一个静态的128字节内存泄漏:
=================================================================
==99442==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 128 byte(s) in 1 object(s) allocated from:
#0 0x7f7a9d2e5e4b in realloc.part.0 (/lib64/libasan.so.8+0xe5e4b) (BuildId: 25975f766867e9e604dc5a71a8befeaed3301942)
#1 0x7b7a9517d302 (<unknown module>)
#2 0x7b7a9517d7e6 (<unknown module>)
#3 0x7f7a9d084773 in __pthread_once_slow.isra.0 (/lib64/libc.so.6+0x77773) (BuildId: abd2a4d896fed122b3f7da571746f193feeb86a1)
#4 0x7f7a9d0847e8 in ___pthread_once (/lib64/libc.so.6+0x777e8) (BuildId: abd2a4d896fed122b3f7da571746f193feeb86a1)
#5 0x7b7a95688cfd (<unknown module>)
#6 0x7b7a955e3c46 (<unknown module>)
#7 0x7b7a9569c5a2 (<unknown module>)
#8 0x7b7a955e48b6 (<unknown module>)
#9 0x7b7a94ca2651 (<unknown module>)
#10 0x7b7a9548a70c (<unknown module>)
#11 0x7b7a94ca6cbc (<unknown module>)
#12 0x7f7a9b927841 (<unknown module>)
#13 0x7f7a9b931520 (<unknown module>)
#14 0x7f7a9b928167 (<unknown module>)
#15 0x7f7a9b917c37 (<unknown module>)
#16 0x7f7a9c78bd1c in SDL_EGL_LoadLibrary (/MyPath/build/libSDL3.so.0+0x18bd1c) (BuildId: be6e280125f40b0956ced6bb4a4a6203e4be8502)
#17 0x7f7a9c8901b6 in Wayland_GLES_LoadLibrary (/MyPath/build/libSDL3.so.0+0x2901b6) (BuildId: be6e280125f40b0956ced6bb4a4a6203e4be8502)
#18 0x7f7a9c7c75d3 in SDL_GL_LoadLibrary_REAL (/MyPath/libSDL3.so.0+0x1c75d3) (BuildId: be6e280125f40b0956ced6bb4a4a6203e4be8502)
#19 0x7f7a9c7c0eed in SDL_ReconfigureWindowInternal (/MyPath/build/libSDL3.so.0+0x1c0eed) (BuildId: be6e280125f40b0956ced6bb4a4a6203e4be8502)
#20 0x7f7a9c7c157c in SDL_ReconfigureWindow (/MyPath/build/libSDL3.so.0+0x1c157c) (BuildId: be6e280125f40b0956ced6bb4a4a6203e4be8502)
#21 0x7f7a9c6a602e in GL_CreateRenderer (/MyPath/build/libSDL3.so.0+0xa602e) (BuildId: be6e280125f40b0956ced6bb4a4a6203e4be8502)
#22 0x7f7a9c687250 in SDL_CreateRendererWithProperties_REAL (/MyPath/build/libSDL3.so.0+0x87250) (BuildId: be6e280125f40b0956ced6bb4a4a6203e4be8502)
#23 0x7f7a9c687821 in SDL_CreateRenderer_REAL (/MyPath/build/libSDL3.so.0+0x87821) (BuildId: be6e280125f40b0956ced6bb4a4a6203e4be8502)
#24 0x7f7a9c686a66 in SDL_CreateWindowAndRenderer_REAL (/MyPath/build/libSDL3.so.0+0x86a66) (BuildId: be6e280125f40b0956ced6bb4a4a6203e4be8502)
#25 0x7f7a9c639224 in SDL_CreateWindowAndRenderer (/MyPath/build/libSDL3.so.0+0x39224) (BuildId: be6e280125f40b0956ced6bb4a4a6203e4be8502)
#26 0x000000400620 in main (/MyPath/build/display+0x400620) (BuildId: 6a0c4123012206b98fcebf6f0bd6b20cee0a2f33)
#27 0x7f7a9d0105b4 in __libc_start_call_main (/lib64/libc.so.6+0x35b4) (BuildId: abd2a4d896fed122b3f7da571746f193feeb86a1)
#28 0x7f7a9d010667 in __libc_start_main@@GLIBC_2.34 (/lib64/libc.so.6+0x3667) (BuildId: abd2a4d896fed122b3f7da571746f193feeb86a1)
#29 0x000000400454 in _start (/MyPath/build/display+0x400454) (BuildId: 6a0c4123012206b98fcebf6f0bd6b20cee0a2f33)
SUMMARY: AddressSanitizer: 128 byte(s) leaked in 1 allocation(s).
Project configuration
为了配置项目,我使用了下面的CMake文件:
cmake_minimum_required(VERSION 3.16)
project(test)
# set the output directory for built objects.
# This makes sure that the dynamic library goes into the build directory automatically.
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$<CONFIGURATION>")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$<CONFIGURATION>")
# This assumes the SDL source is available in vendored/SDL3-3.4.8
add_subdirectory(vendored/SDL3-3.4.8 EXCLUDE_FROM_ALL)
# Create your game executable target as usual
add_executable(test
src/test.c
)
target_compile_options(test PRIVATE -fsanitize=address)
target_link_options(test PRIVATE -fsanitize=address)
# Link to the actual SDL3 library.
target_link_libraries(test PRIVATE SDL3::SDL3)
并且我运行了这个命令,因为我还没有安装SDL_X11_XSCRNSAVER
cmake -B build -DSDL_X11_XSCRNSAVER=OFF &&
cmake --build build
Where do you this the bug come from?
你认为这个Bug出自哪里?
我看到 SDL 抱怨大量内存泄漏来自外部库,因此我更愿意先在这里提问。
解决方案
某些外部库(在你的环境中很可能是OpenGL驱动)会进行内存分配但从不释放,通常在再次使用该库时会重用这些分配。
这个泄漏是发生一次还是两次?
#include <SDL3/SDL.h>
int main() {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_CreateWindowAndRenderer("Leak Test", 640, 480, 0, &window, &renderer);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
window = NULL;
renderer = NULL;
SDL_CreateWindowAndRenderer("Leak Test", 640, 480, 0, &window, &renderer);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。