当前位置:  开发笔记 > 运维 > 正文

SDL库内存泄漏

如何解决《SDL库内存泄漏》经验,为你挑选了1个好方法。

我试图确保我的应用程序中没有内存泄漏.我将泄漏范围缩小到以下简单的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)

Prof. Falken.. 6

不幸的是,库有内存泄漏的情况并不少见.只要它不是那种不断增长的类型,它就是丑陋但没什么大不了的.应用程序退出时,操作系统将释放内存.(至少是Windows和Linux等操作系统.)



1> Prof. Falken..:

不幸的是,库有内存泄漏的情况并不少见.只要它不是那种不断增长的类型,它就是丑陋但没什么大不了的.应用程序退出时,操作系统将释放内存.(至少是Windows和Linux等操作系统.)

推荐阅读
贴进你的心聆听你的世界
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有