EDIT:更新了一些新信息(粗体)。此外,代码和 Valgrinds 输出也已更新。
我最近开始使用 SDL2 作为我的图形库。
开发了一些东西后,我决定运行 Valgrind 并发现我正在泄漏内存......大量内存。
缩小范围后我编译了这段代码(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;
}
这是生成文件:
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 字节,而不是 200,000!)。另外,我检查了互联网上的其他示例,尝试在我的计算机上运行它们,它们都有相同的泄漏(我假设是 SDL_Init)。
我正在 Ubuntu 13-64 位上运行。