系统相关
首页 > 系统相关> > c – SDL_Init中的大内存泄漏

c – SDL_Init中的大内存泄漏

作者:互联网

编辑:更新了一些新信息(Bold’ed).此外,代码和Valgrinds输出已更新.

我最近开始使用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个字节,而不是200,000!).
此外,我检查了互联网上的其他示例,尝试在我的计算机上运行它们,并且它们都有相同的泄漏(从我假设的是SDL_Init).

我在Ubuntu13-64Bit上运行.

解决方法:

是的,SDL中存在一些泄漏,但它可能来自不同的地方,其中一些并不是SDL的错.

例如,我的视频卡驱动程序(nvidia)泄漏了大量的10Mb内存. X11也因一些大泄漏而闻名.我不会那么担心,有一些你无法控制的事情.

在你的具体情况下,我会用标志运行valgrind –leak-check = full –track-originins = yes –show-reachable = yes,查看泄漏是否真的来自SDL并在其中发布有关它的错误http://bugzilla.libsdl.org如果尚未报告.

标签:sdl-2,c,memory-leaks
来源: https://codeday.me/bug/20190825/1714995.html