c++ - SDL TTF 库内存泄漏(每帧创建和释放曲面)

标签 c++ opengl memory-leaks sdl sdl-ttf

我正在为我正在制作的游戏创建一个基本的 SDL+OpenGL 关卡编辑器,在创建动态 TTF 表面,然后将它们转换为 OpenGL 纹理时,我遇到了一些非常严重的内存泄漏。

例如:

每一帧,我都运行一些如下所示的代码:

shape_label = SDL_DisplayFormat(TTF_RenderText_Solid(font, shape_names[c2].c_str(), textColor));
shape_label_gl = gl_texture(shape_label);
draw_rect(shapes[c2][0], shapes[c2][1], shape_label->w, shape_label->h, shape_label_gl, 0);
SDL_FreeSurface(shape_label);

但是,当我使用 valgrind 时,它提示有很多相对较大的内存泄漏。当我在 Activity Monitor (Mac) 中监控该程序时,它可能会攀升至近 500 mb 的内存使用量,并且可能会从那里继续。

这是 valgrind 错误:

==61330== 1,193,304 (13,816 direct, 1,179,488 indirect) bytes in 157 blocks are definitely lost in loss record 6,944 of 6,944
==61330==    at 0xB823: malloc (vg_replace_malloc.c:266)
==61330==    by 0x4D667: SDL_CreateRGBSurface (in /Library/Frameworks/SDL.framework/Versions/A/SDL)
==61330==    by 0xE84C3: TTF_RenderUNICODE_Solid (in /Library/Frameworks/SDL_ttf.framework/Versions/A/SDL_ttf)
==61330==    by 0xE836D: TTF_RenderText_Solid (in /Library/Frameworks/SDL_ttf.framework/Versions/A/SDL_ttf)
==61330==    by 0x10000A06D: SDL_main (in ./leveleditor)
==61330==    by 0x100013530: -[SDLMain applicationDidFinishLaunching:] (in ./leveleditor)
==61330==    by 0x65AD0D: __-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke_1 (in /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation)
==61330==    by 0x36C7B9: _CFXNotificationPost (in /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation)
==61330==    by 0x646FC2: -[NSNotificationCenter postNotificationName:object:userInfo:] (in /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation)
==61330==    by 0xB4C4E2: -[NSApplication _postDidFinishNotification] (in /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit)
==61330==    by 0xB4C248: -[NSApplication _sendFinishLaunchingNotification] (in /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit)
==61330==    by 0xB4AF0F: -[NSApplication(NSAppleEventHandling) _handleAEOpenEvent:] (in /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit)

关于如何在不使用大量内存的情况下使 TTF 逐帧工作有什么想法吗?

编辑:为了将来引用,我是个白痴。我忘了这样做:

glDeleteTextures(1, &status_bottom_gl);

最佳答案

TTF_RenderText_Solid 分配一个您没有释放的新表面:

SDL_Surface *ttf = TTF_RenderText_Solid(font, shape_names[c2].c_str(), textColor);
shape_label = SDL_DisplayFormat(ttf);
shape_label_gl = gl_texture(shape_label);
draw_rect(shapes[c2][0], shapes[c2][1], shape_label->w, shape_label->h, shape_label_gl, 0);
SDL_FreeSurface(shape_label);
SDL_FreeSurface(ttf); // Must free this as well!

请注意,为了获得最佳性能,您应该将这些表面缓存在某个地方,而不是每帧都创建和销毁它们。

关于c++ - SDL TTF 库内存泄漏(每帧创建和释放曲面),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11484895/

相关文章:

java - OpenGL + 处理 : Rotate and move based on direction

c - OpenGL - 顶点结构对齐到 32 字节?

memory-management - 关闭内存泄漏

java - Jetty 7.4.5 服务器在 useFileMappedBuffer = true 时泄漏文件描述符

c++ - 内存使用率上升。释放的内存未被重用

c++ - 我的多线程游戏一直处于 100% CPU 状态。如何管理线程事件以减少 CPU 负载?

c++ - C++ 中是否有一个简单且开源的光线转换库?

c++ - 使用 NDC 空间在近平面上投影点

c++ - CPLEX MIP 内存不足 : NodeFileInd is 2 but no files has been created

c++ - QQmlList属性<T> : Why does the following getter function work?