The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
A memory leak because text rendering
Alex0451


Joined: 10 Dec 2015
Posts: 1
Location: Ukraine
Hi guys.
I'm writing a little game, and there is a problem. If do not call the rendering of text, the Windows task managers shows that my game uses about 5 MB. If enable renderring of the text, then memory usage immediately increased to 8 MB and grows (20, 40, 60 mb ... etc).
I need help with finding memory leak.
I downloaded the latest version of the SDL and its extensions. Using the code from the text drawing tutorial. And still leaking.

Here's the code of text rendering function:
Code:

   void DrawText() {
      SDL_Color    color = {200,200,200};
      string       textHP  = "HP: "+to_string(playerShip->hp);
      string       textSH  = "SH: " + to_string(playerShip->shield);
      SDL_Surface* surfText1 = TTF_RenderText_Solid(gFont,textHP.c_str(),color);
      SDL_Texture* texText1  = SDL_CreateTextureFromSurface(render,surfText1);
      SDL_Rect    rctText1;
      rctText1.y=10;
      rctText1.x=10;
      rctText1.w=50;
      rctText1.h=20;
      SDL_RenderCopy(render,texText1,nullptr,&rctText1);
      SDL_FreeSurface(surfText1);
      SDL_DestroyTexture(texText1);

      surfText1 = TTF_RenderText_Solid(gFont,textSH.c_str(),color);
      texText1  = SDL_CreateTextureFromSurface(render,surfText1);
      rctText1.y+=rctText1.h;
      SDL_RenderCopy(render,texText1,nullptr,&rctText1);
      SDL_FreeSurface(surfText1);
      SDL_DestroyTexture(texText1);
   }


And function is called in a loop drawing:
Code:

while(quit != true){
         UpdateGame();
         HandleInput();
         
         SDL_SetRenderDrawColor(gRender, 0, 15, 23, 1);
         SDL_RenderClear(render);
         DrawGraphic();
         DrawText();
         SDL_RenderPresent(render);
         SDL_Delay(2);

         time++;
      }