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
Massive Memory Leaks
dwrj87


Joined: 09 Dec 2014
Posts: 3
Hi

I've been trying to learn C++ and SDL, and seem to have reached an empasse. Even with this simple example, my program seems to be losing about 1 MB memory a *second*. This is after I've trimmed it down to try and locate the leak. I'd guess that I'm doing something majorly wrong for this to happen, but I can't figure out what. Can anybody here help me please?

Code:
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
            showMessage(2, "SDL_Init() Failed: " + (string) SDL_GetError());
            return 1;
        }
        if (TTF_Init() != 0) {
            showMessage(2, "TTF_Init() Failed: " + (string) TTF_GetError());
            SDL_Quit();
            return 1;
        }

        SDL_Window *window;
        SDL_Renderer *renderer;
        SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL, &window, &renderer);
       
        SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
        SDL_RenderSetLogicalSize(renderer, WINDOW_WIDTH, WINDOW_HEIGHT);

        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
       
        SDL_Event event;
       
        long currentFrame = 0;
       
        TTF_Font *font = TTF_OpenFont("/usr/share/fonts/truetype/freefont/FreeSans.ttf", 28);
               
        while (!quitMainLoop) {
           
            currentFrame++;
                       
            SDL_RenderClear(renderer);
           
            SDL_PollEvent(&event);

            switch (event.type) {
                case SDL_KEYDOWN:                   
                    switch (event.key.keysym.sym) {
                        case SDLK_ESCAPE:
                            quitMainLoop = true;
                            break;
                    }
                    break;
                case SDL_QUIT:
                    quitMainLoop = true;
                    break;
            }
                           
            output = "Hello " + to_string(currentFrame);
           
            SDL_Surface *surface = TTF_RenderText_Solid(font, output.c_str(), COLOUR_WHITE);
            SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
            SDL_FreeSurface(surface);

            SDL_QueryTexture(texture, NULL, NULL, &textureWidth, &textureHeight);

            SDL_Rect dstrect {0, 0, textureHeight, textureWidth};

            SDL_RenderCopy(renderer, texture, NULL, &dstrect);
            SDL_DestroyTexture(texture);
           
           
            SDL_RenderPresent(renderer);
       
        }
           
        TTF_CloseFont(font);
        TTF_Quit();
        SDL_Quit();
[/code]
MrTAToad


Joined: 13 Feb 2014
Posts: 205
Location: Chichester, England
After rendering the text, you need to destroy the texture and surface - each TTF call creates a new surface!
dwrj87


Joined: 09 Dec 2014
Posts: 3
I have SDL_FreeSurface(surface); and SDL_DestroyTexture(texture); in there. Is there something else I should be doing to destroy them?
Naith


Joined: 03 Jul 2014
Posts: 158
Try moving 'SDL_FreeSurface(surface)' to be called after 'SDL_RenderCopy(renderer, ....)' and before 'SDL_DestroyTexture(texture)', i.e:

Code:

   SDL_RenderCopy(renderer, texture, NULL, &dstrect);

   SDL_FreeSurface(surface);
   SDL_DestroyTexture(texture);



Also, to optimize the code, you should insert the SDL_PollEvent-call into a while loop so it's only called whenever a event is occuring, i.e:

Code:

   while(SDL_PollEvent(&event))
   {
      switch(event.type)
      {
      case SDL_QUIT:
         {
            quitMainLoop = true;

            break;
         }

      case SDL_KEYDOWN:
         {
            quitMainLoop = true;

            break;
         }
      }
   }

   // Rest of code (create surface, texture and such)

MrTAToad


Joined: 13 Feb 2014
Posts: 205
Location: Chichester, England
Ah yes - didn't see them there Laughing

Is the output variable a std::string or something else ? If its the latter, perhaps you need to free that.
dwrj87


Joined: 09 Dec 2014
Posts: 3
Yes, output is a std::string

I have added a while loop around the event polling

I'm also afraid that the memory leak has disappeared. Posted the code today once my group membership was confirmed, ran it for the first time in a few days and no leak :S I'm afraid that I really can't explain it sorry :S
Naith


Joined: 03 Jul 2014
Posts: 158
Glad it turned out alright. =)