gpt4 book ai didi

c - SDL 库内存泄漏

转载 作者:太空狗 更新时间:2023-10-29 16:08:57 25 4
gpt4 key购买 nike

我正在努力确保我的应用程序中没有内存泄漏。我将泄漏范围缩小到以下简单的 SDL 应用程序:

int main(int argc, char* argv[]) {

if( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) != 0) {
printf("Could not initialize SDL - %s\n", SDL_GetError());
return -1;
}

int flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL|SDL_FULLSCREEN;
SDL_Surface* screen = SDL_SetVideoMode(0, 0, 0, flags);

SDL_Quit();
return 0;
}

当我在此应用程序上运行 valgrind 时,它报告了一堆内存泄漏,如本消息末尾所列。

请注意,根据 SDL_SetVideoMode 上的文档,从 SDL_SetVideoMode 返回的表面由 SDL_Quit 释放,调用者不得释放。

http://sdl.beuc.net/sdl.wiki/SDL_SetVideoMode

如果有人能告诉我我缺少什么,我将不胜感激。

预先感谢您的帮助。

    ==651== Memcheck, a memory error detector
==651== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==651== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
==651== Command: ./main
==651==
==651== Syscall param writev(vector[...]) points to uninitialised byte(s)
==651== at 0x4F3FE05: writev (writev.c:56)
==651== by 0x40004: ???
==651== Address 0x5701bfb is 19 bytes inside a block of size 16,384 alloc'd
==651== at 0x402732C: calloc (vg_replace_malloc.c:467)
==651== by 0x5B06551: XOpenDisplay (in /usr/lib/i386-linux-gnu/libX11.so.6.3.0)
==651== by 0x4DF52DB: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4E00403: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4DED4A8: SDL_VideoInit (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4DC1C79: SDL_InitSubSystem (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4E8F112: (below main) (libc-start.c:226)
==651==
==651==
==651== HEAP SUMMARY:
==651== in use at exit: 48,065 bytes in 917 blocks
==651== total heap usage: 13,115 allocs, 12,198 frees, 1,985,815 bytes allocated
==651==
==651== 10 bytes in 2 blocks are definitely lost in loss record 9 of 167
==651== at 0x4028876: malloc (vg_replace_malloc.c:236)
==651== by 0x4EEC010: strdup (strdup.c:43)
==651== by 0xBED27FFF: ???
==651==
==651== 12 bytes in 1 blocks are definitely lost in loss record 14 of 167
==651== at 0x4028876: malloc (vg_replace_malloc.c:236)
==651== by 0x5C2206A: ???
==651== by 0x5C21AE4: ???
==651== by 0x5C21E8A: ???
==651== by 0x5C22365: ???
==651== by 0x5C1C8AD: ???
==651== by 0x5C1CBAB: ???
==651== by 0x4DF62CB: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4E006CF: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4DED4A8: SDL_VideoInit (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4DC1C79: SDL_InitSubSystem (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4E8F112: (below main) (libc-start.c:226)
==651==
==651== 112 (8 direct, 104 indirect) bytes in 1 blocks are definitely lost in loss record 135 of 167
==651== at 0x402896C: realloc (vg_replace_malloc.c:525)
==651== by 0x5B2A7B6: ??? (in /usr/lib/i386-linux-gnu/libX11.so.6.3.0)
==651== by 0x5B2ACB2: ??? (in /usr/lib/i386-linux-gnu/libX11.so.6.3.0)
==651== by 0x5B2C75F: ??? (in /usr/lib/i386-linux-gnu/libX11.so.6.3.0)
==651== by 0x5B2CFEA: _XlcCreateLC (in /usr/lib/i386-linux-gnu/libX11.so.6.3.0)
==651== by 0x5B4BFD9: _XlcDefaultLoader (in /usr/lib/i386-linux- gnu/libX11.so.6.3.0)
==651== by 0x5B34E3B: _XOpenLC (in /usr/lib/i386-linux-gnu/libX11.so.6.3.0)
==651== by 0x5B34FA1: _XlcCurrentLC (in /usr/lib/i386-linux-gnu/libX11.so.6.3.0)
==651== by 0x4E005DB: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4DED4A8: SDL_VideoInit (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4DC1C79: SDL_InitSubSystem (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4E8F112: (below main) (libc-start.c:226)
==651==
==651== 112 (8 direct, 104 indirect) bytes in 1 blocks are definitely lost in loss record 136 of 167
==651== at 0x402896C: realloc (vg_replace_malloc.c:525)
==651== by 0x5B2A7B6: ??? (in /usr/lib/i386-linux-gnu/libX11.so.6.3.0)
==651== by 0x5B2ACB2: ??? (in /usr/lib/i386-linux-gnu/libX11.so.6.3.0)
==651== by 0x5B2C75F: ??? (in /usr/lib/i386-linux-gnu/libX11.so.6.3.0)
==651== by 0x5B2CFEA: _XlcCreateLC (in /usr/lib/i386-linux-gnu/libX11.so.6.3.0)
==651== by 0x5B4BFD9: _XlcDefaultLoader (in /usr/lib/i386-linux- gnu/libX11.so.6.3.0)
==651== by 0x5B34E3B: _XOpenLC (in /usr/lib/i386-linux-gnu/libX11.so.6.3.0)
==651== by 0x5B350BA: _XrmInitParseInfo (in /usr/lib/i386-linux- gnu/libX11.so.6.3.0)
==651== by 0x5B1B6AD: ??? (in /usr/lib/i386-linux-gnu/libX11.so.6.3.0)
==651== by 0x5B1F294: XrmGetStringDatabase (in /usr/lib/i386-linux-gnu/libX11.so.6.3.0)
==651== by 0x5AFA4DA: ??? (in /usr/lib/i386-linux-gnu/libX11.so.6.3.0)
==651== by 0x5AFA6F6: XGetDefault (in /usr/lib/i386-linux-gnu/libX11.so.6.3.0)
==651==
==651== 124 bytes in 1 blocks are definitely lost in loss record 137 of 167
==651== at 0x4028876: malloc (vg_replace_malloc.c:236)
==651== by 0x55B0747: ??? (in /usr/lib/i386-linux-gnu/libxcb.so.1.1.0)
==651== by 0x55B0802: ??? (in /usr/lib/i386-linux-gnu/libxcb.so.1.1.0)
==651== by 0x55B029F: xcb_connect_to_display_with_auth_info (in /usr/lib/i386- linux-gnu/libxcb.so.1.1.0)
==651== by 0x55B045A: xcb_connect (in /usr/lib/i386-linux-gnu/libxcb.so.1.1.0)
==651== by 0x5B16407: _XConnectXCB (in /usr/lib/i386-linux-gnu/libX11.so.6.3.0)
==651== by 0x5B0619F: XOpenDisplay (in /usr/lib/i386-linux-gnu/libX11.so.6.3.0)
==651== by 0x4DF52DB: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4E003DF: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4DED4A8: SDL_VideoInit (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4DC1C79: SDL_InitSubSystem (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4E8F112: (below main) (libc-start.c:226)
==651==
==651== 124 bytes in 1 blocks are definitely lost in loss record 138 of 167
==651== at 0x4028876: malloc (vg_replace_malloc.c:236)
==651== by 0x55B0747: ??? (in /usr/lib/i386-linux-gnu/libxcb.so.1.1.0)
==651== by 0x55B0802: ??? (in /usr/lib/i386-linux-gnu/libxcb.so.1.1.0)
==651== by 0x55B029F: xcb_connect_to_display_with_auth_info (in /usr/lib/i386-linux-gnu/libxcb.so.1.1.0)
==651== by 0x55B045A: xcb_connect (in /usr/lib/i386-linux-gnu/libxcb.so.1.1.0)
==651== by 0x5B16407: _XConnectXCB (in /usr/lib/i386-linux-gnu/libX11.so.6.3.0)
==651== by 0x5B0619F: XOpenDisplay (in /usr/lib/i386-linux-gnu/libX11.so.6.3.0)
==651== by 0x4DF52DB: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4DFDD7B: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4DED574: SDL_VideoInit (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4DC1C79: SDL_InitSubSystem (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4E8F112: (below main) (libc-start.c:226)
==651==
==651== 124 bytes in 1 blocks are definitely lost in loss record 139 of 167
==651== at 0x4028876: malloc (vg_replace_malloc.c:236)
==651== by 0x55B0747: ??? (in /usr/lib/i386-linux-gnu/libxcb.so.1.1.0)
==651== by 0x55B0802: ??? (in /usr/lib/i386-linux-gnu/libxcb.so.1.1.0)
==651== by 0x55B029F: xcb_connect_to_display_with_auth_info (in /usr/lib/i386-linux-gnu/libxcb.so.1.1.0)
==651== by 0x55B045A: xcb_connect (in /usr/lib/i386-linux-gnu/libxcb.so.1.1.0)
==651== by 0x5B16407: _XConnectXCB (in /usr/lib/i386-linux-gnu/libX11.so.6.3.0)
==651== by 0x5B0619F: XOpenDisplay (in /usr/lib/i386-linux-gnu/libX11.so.6.3.0)
==651== by 0x4DF52DB: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4E00403: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4DED4A8: SDL_VideoInit (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4DC1C79: SDL_InitSubSystem (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4E8F112: (below main) (libc-start.c:226)
==651==
==651== 124 bytes in 1 blocks are definitely lost in loss record 140 of 167
==651== at 0x4028876: malloc (vg_replace_malloc.c:236)
==651== by 0x55B0747: ??? (in /usr/lib/i386-linux-gnu/libxcb.so.1.1.0)
==651== by 0x55B0802: ??? (in /usr/lib/i386-linux-gnu/libxcb.so.1.1.0)
==651== by 0x55B029F: xcb_connect_to_display_with_auth_info (in /usr/lib/i386-linux-gnu/libxcb.so.1.1.0)
==651== by 0x55B045A: xcb_connect (in /usr/lib/i386-linux-gnu/libxcb.so.1.1.0)
==651== by 0x5534050: pa_client_conf_from_x11 (in /usr/lib/i386-linux-gnu/libpulsecommon-1.0.so)
==651== by 0x54172E4: pa_context_new_with_proplist (in /usr/lib/i386-linux-gnu/libpulse.so.0.13.4)
==651== by 0x54173EA: pa_context_new (in /usr/lib/i386-linux-gnu/libpulse.so.0.13.4)
==651== by 0x54085C5: pa_simple_new (in /usr/lib/i386-linux-gnu/libpulse-simple.so.0.0.3)
==651== by 0x4DF4263: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4DC2B16: SDL_AudioInit (in /usr/lib/libSDL-1.2.so.0.11.3)
==651== by 0x4DC1BD6: SDL_InitSubSystem (in /usr/lib/libSDL-1.2.so.0.11.3)
==651==
==651== LEAK SUMMARY:
==651== definitely lost: 534 bytes in 9 blocks
==651== indirectly lost: 208 bytes in 8 blocks
==651== possibly lost: 0 bytes in 0 blocks
==651== still reachable: 47,323 bytes in 900 blocks
==651== suppressed: 0 bytes in 0 blocks
==651== Reachable blocks (those to which a pointer was found) are not shown.
==651== To see them, rerun with: --leak-check=full --show-reachable=yes
==651==
==651== For counts of detected and suppressed errors, rerun with: -v
==651== Use --track-origins=yes to see where uninitialised values come from
==651== ERROR SUMMARY: 10 errors from 9 contexts (suppressed: 113 from 13)

最佳答案

不幸的是,库内存泄漏的情况并不少见。只要不是一直长的那种,虽然丑但没什么大不了的。操作系统将在应用程序退出时释放内存。 (至少是 Windows 和 Linux 等操作系统。)

关于c - SDL 库内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8451419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com