编辑:更新了一些新信息(加粗)。此外,代码和Valgrind输出也已更新。
最近我开始使用SDL2作为我的图形库。在开发一些东西后,我决定运行Valgrind并发现我正在泄漏内存......大量的内存。
经过缩小范围,我编译了这段代码(用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;
}
这是 make 文件:
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)
我得到了这个Valgrind错误:
==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)
我看到很多人抱怨SDL中的内存泄漏问题,但它们都非常小(约为16个字节,而不是20万个!)。 此外,我还检查了来自互联网的其他示例,试图在我的计算机上运行它们,它们都有相同的泄漏问题(我想这是因为SDL_Init引起的)。
我正在运行Ubuntu13-64位系统。
SDL_Quit()
之前关闭所有子系统。即:SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
- WhozCraig