3

I am trying to ensure that there are no memory leaks in my application. I narrowed down the leak to the following simple SDL application:

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;
}

When I run valgrind on this application, it reports a bunch of memory leaks as listed at the end of this message.

Note that, according to the docs on SDL_SetVideoMode, the returned surface from SDL_SetVideoMode is freed by SDL_Quit and must not be freed by the caller.

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

I would appreciate it if someone can tell me what is it that I am missing.

Thank you in advance for your help.

    ==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)
Peter
  • 11,260
  • 14
  • 78
  • 155

1 Answers1

6

Unfortunately, it's not uncommon for libraries to have memory leaks. As long as it's not the ever-growing kind, it's ugly but no big deal. The OS will free the memory upon application exit. (At least operating systems such as Windows and Linux.)

Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
  • 1
    Thank you for your help. I understand the general problem. I was looking for some confirmation that it is not something in my code that I am missing. Either that or some workaround that people have used in the past. – Peter Dec 10 '11 at 00:11