Why does valgrind say basic SDL program is leaking memory?

前端 未结 5 456
星月不相逢
星月不相逢 2020-12-01 12:00

Here is the SDL program:

#include 

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


  SDL_Init(SDL_INIT_VIDEO);
  SDL_Surface* screen = SDL_SetVideoMode(         


        
相关标签:
5条回答
  • 2020-12-01 12:36

    Singletons are pretty much always a 'leak' with standard implementations. That is usually ok, though, since normally it's not like you want to unload your ability to do things like print to the console.

    0 讨论(0)
  • 2020-12-01 12:41

    SDL definitely has a memory leak issue with SDL_TLSCleanup and SDL_TLSData.

    In fact, SDL_TLSCleanup is never called for the main thread.

    0 讨论(0)
  • 2020-12-01 12:46

    Even for basic OpenGL "hello world" program without the full SDL, Valgrind gives me similar warnings deep inside the OpenGL libraries. It's peculiar, but I've assumed

    • The library implementors know what they're doing (probably preallocating some small static buffers they never bother to free),
    • Even if they don't, it's a one-time leak that'll be reclaimed by the OS when the program terminates,

    and haven't lost much sleep over it.

    0 讨论(0)
  • 2020-12-01 12:55

    This is normal for the graphics API library (OpenGL, Vulkan, etc) and windowing API (X11, SDL, etc). This best option is to use valgrind suppression files to ignore these errors.

    Here is how to do it with Linux, SDL & OpenGL:

    Let's say the program you are debugging is called 'prog.out' (remember to replace prog.out with the actual name of your program),

    You can export suppression info like this:

    valgrind --leak-check=full --show-reachable=yes --show-leak-kinds=all --error-limit=no --gen-suppressions=all --log-file=supdata.log ./prog.out
    

    You can now extract the suppression info from 'supdata.log' using a script or manually. You can use wildcards ('*', '...') to make suppressions more generic (thereby not having to update the suppression file each time you update your code).

    After that, whenever you debug your program you would include the suppression file from now on. For example, I made the suppression file below 'linux_sdl_gl.sup'. It works well for me when developing using SDL and OpenGL on Linux. It ignores all of OpenGL, SDL and X11 built in errors so that I can easily find errors I create. Feel free to use it in your development.

    When I debug my code I use the following call to valgrind. This allows me to find any new system bugs that my suppression file may have missed.

    valgrind --gen-suppressions=all --suppressions=./linux_sdl_gl.sup --leak-check=full --show-leak-kinds=all ./prog.out
    

    file: linux_sdl_gl.sup

    # Copyright (c) <'2019'> <'Alrick Grandison'>
    
    # This software is provided 'as-is', without any express or implied
    # warranty. In no event will the authors be held liable for any damages
    # arising from the use of this software.
    
    # Permission is granted to anyone to use this software for any purpose,
    # including commercial applications, and to alter it and redistribute it
    # freely, subject to the following restrictions:
    
    # 1. The origin of this software must not be misrepresented; you must not
    #    claim that you wrote the original software. If you use this software
    #    in a product, an acknowledgment in the product documentation would be
    #    appreciated but is not required.
    # 2. Altered source versions must be plainly marked as such, and must not be
    #    misrepresented as being the original software.
    # 3. This notice may not be removed or altered from any source distribution.
    
    {
       opengl_memcpy_addr8
       Memcheck:Addr8
       fun:memcpy@GLIBC*
       obj:/usr/lib/x86_64-linux-gnu/dri/*_dri.so
       ...
    }
    
    {
       opengl_memcpy_addr1
       Memcheck:Addr1
       fun:memcpy@GLIBC*
       obj:/usr/lib/x86_64-linux-gnu/dri/*_dri.so
       ...
    }
    
    {
       opengl_memset_addr8
       Memcheck:Addr8
       fun:memset
       obj:/usr/lib/x86_64-linux-gnu/dri/*_dri.so
       ...
    }
    
    {
       sdl_leak_reachable
       Memcheck:Leak
       match-leak-kinds: reachable
       ...
       fun:SDL_Init_REAL
       ...
    }
    
    {
       x11_leak_reachable
       Memcheck:Leak
       match-leak-kinds: reachable
       ...
       obj:/usr/lib/x86_64-linux-gnu/libX11.so.*
       ...
    }
    
    {
       sdl_leak_indirect
       Memcheck:Leak
       match-leak-kinds: indirect
       ...
       fun:SDL_Init_REAL
       ...
    }
    
    {
      sdl_leak_definite
       Memcheck:Leak
       match-leak-kinds: definite
       ...
       fun:SDL_Init_REAL
       ...
    }
    
    # OpenGL Calls DL under the Hood - Taken straight out of Valgrind --gen-suppressions
    # Could overlap with non-graphics DL api calls
    # But, If you are not using DL directly, then don't worry about this
    {
       dl_leak_reachable
       Memcheck:Leak
       match-leak-kinds: reachable
       ...
       fun:dlopen@@GLIBC*
       ...
    }
    
    # Same as above but more generic
    # Could overlap with non-graphics DL api calls
    # But, If you are not using DL directly, then don't worry about this
    {
       dl_leak_reachable
       Memcheck:Leak
       match-leak-kinds: reachable
       ...
       fun:_dl_*
       ...
    }
    
    {
       x11_leak_indirect
       Memcheck:Leak
       match-leak-kinds: indirect
       ...
       obj:/usr/lib/x86_64-linux-gnu/libX11.so.*
       ...
    }
    
    {
       x11_leak_definite
       Memcheck:Leak
       match-leak-kinds: definite
       ...
       obj:/usr/lib/x86_64-linux-gnu/libX11.so.*
       ...
    }
    
    {
       x11_leak_possible
       Memcheck:Leak
       match-leak-kinds: possible
       ...
       obj:/usr/lib/x86_64-linux-gnu/libX11.so.*
       ...
    }
    
    {
       opengl_leak_reachable
       Memcheck:Leak
       match-leak-kinds: reachable
       ...
       obj:/usr/lib/x86_64-linux-gnu/libGLX.so.*
       ...
    }
    
    

    github: valgrind supression code

    0 讨论(0)
  • 2020-12-01 12:55

    Every SDL_Surface you load should be freed before the call of SDL_Quit().

    To do this, just use SDL_FreeSurface(surfaceName) to free up the surface that you allocated on memory.

    0 讨论(0)
提交回复
热议问题