title: SDL TTF Library Memory Leaks (Creating and Freeing Surfaces Every Frame)
body: <p>I am creating a basic SDL+OpenGL level editor for a game I'm making, and I'm encountering some pretty severe memory leaks when creating dynamic TTF surfaces, then converting them to OpenGL textures.</p>
  
  <p>For example:</p>
  
  <p>Every frame, I run some code that looks like this:</p>
  
  <pre><code>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-&gt;w, shape_label-&gt;h, shape_label_gl, 0);
  SDL_FreeSurface(shape_label);
  </code></pre>
  
  <p>However, when I use valgrind it suggests lots of relatively large memory leaks. When I monitor the program in Activity Monitor (Mac), it can climb to almost 500 mb of memory usage, and would probably continue from there.</p>
  
  <p>Here's the valgrind error(s):</p>
  
  <pre><code>==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)
  </code></pre>
  
  <p>Any ideas on how to make TTF work frame-to-frame without using huge amounts of memory?</p>
  
  <p><strong>EDIT:</strong> For future reference, I am a moron. I forgot to do this:</p>
  
  <pre><code>glDeleteTextures(1, &amp;status_bottom_gl);
  </code></pre>