Big memory leak in SDL_Init

2019-05-11 18:36发布

问题:

EDIT: updated with some new info (Bold'ed). Also, the code and Valgrinds output is updated.

I recently started using SDL2 as my graphics library. After developing some stuff, I decided to run Valgrind and found out that I am leaking memory... a lot of memory.

After narrowing it down I compiled this code (In C):

#include <SDL2/SDL.h>

int main(int argc, char** argv)
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
    SDL_Quit();

    return 0;
}

This is the make file:

CC = gcc
CCFLAGS = -Wall -o0
LDFLAGS = -lSDL2
SOURCES= main.c
OBJECTS=$(SOURCES:.c=.o)
EXE = Test

.PHONY:
all: $(OBJECTS)
    $(CC) $(OBJECTS) $(CCFLAGS) $(LDFLAGS) -o $(EXE)

clean:
    rm $(OBJECTS) $(EXE)

And got this Valgrind error:

==30933== Memcheck, a memory error detector
==30933== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==30933== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==30933== Command: ./Test
==30933== 
==30933== 
==30933== HEAP SUMMARY:
==30933==     in use at exit: 308,407 bytes in 559 blocks
==30933==   total heap usage: 9,346 allocs, 8,787 frees, 2,502,489 bytes allocated
==30933== 
==30933== LEAK SUMMARY:
==30933==    definitely lost: 197,226 bytes in 6 blocks
==30933==    indirectly lost: 6,272 bytes in 8 blocks
==30933==      possibly lost: 0 bytes in 0 blocks
==30933==    still reachable: 104,909 bytes in 545 blocks
==30933==         suppressed: 0 bytes in 0 blocks
==30933== Rerun with --leak-check=full to see details of leaked memory
==30933== 
==30933== For counts of detected and suppressed errors, rerun with: -v
==30933== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 1)

I looked around and saw many people complain about memory leaks in SDL, but they all were very small (about 16 bytes, not 200,000!). Also, I checked other examples from the internet, trying to run them on my computer, and they all had that same leak (from what I'm assuming is SDL_Init).

I am running on Ubuntu13-64Bit.

回答1:

Yes, there are some leaks in SDL, but it can come from different places and some of them aren't really SDL's fault.

For instance, my video card driver (nvidia) leaks a good 10Mb of memory. X11 is also known for some big leaks. I wouldn't worry that much, there are some things you can't control.

In your specific case, I would run valgrind with the flags --leak-check=full --track-origins=yes --show-reachable=yes, see if the leak is really coming from SDL and post a bug about it in http://bugzilla.libsdl.org if it isn't already reported.