2012-07-14 78 views
1

我正在爲我製作的遊戲創建基本的SDL + OpenGL級別編輯器,並且在創建動態TTF表面時遇到一些非常嚴重的內存泄漏,然後將它們轉換爲OpenGL紋理。SDL TTF庫內存泄漏(每幀創建和釋放表面)

例如:

每一幀,我跑了一些代碼,看起來像這樣:

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它表明很多比較大的內存泄漏。當我在活動監視器(Mac)中監視程序時,它可能會攀升至近500 MB的內存使用量,並可能從此處繼續。

這裏的Valgrind的錯誤(S):

==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); 

回答

1

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! 

請注意,以獲得最佳性能,你應該某處緩存那些表面,而不是創造&摧毀他們的每一幀。

+0

當我這樣做的時候,它仍然在上升......當我關注opengl Gluint變量時,它每次都穩步上升。有沒有一種方法可以釋放opengl紋理? – 2012-07-14 15:50:20

+0

啊,沒關係。忘了'glDeleteTextures(1,&status_bottom_gl);'等等。 – 2012-07-14 15:55:47