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
My SDL 2 game crashes when it is idle
cyberspace009


Joined: 11 Jun 2015
Posts: 3
Location: Orlando, FL
Hi, SDL Community!

I ran into a problem in my game. When I leave the game idle for a good 45 minutes, this error appears in the terminal:


I am not sure what is causing this error but I do believe the cause is this:
Code:

//Init.cpp file
//load the sprites on the screen
bool Init::render_texture(int x, int y, SDL_Texture* image, SDL_Renderer* render, SDL_Rect* clip)
{
   SDL_Rect renderQuad;

   renderQuad.x = x;
   renderQuad.y = y;

   if (clip != NULL)
   {
      renderQuad.h = clip->h;
      renderQuad.w = clip->w;
   }
   else
   {
      SDL_QueryTexture(image, NULL, NULL, &renderQuad.w, &renderQuad.h);
   }

   SDL_RenderCopy(render, image, clip, &renderQuad);

   return true;
}


Or maybe it is my while loop:
Code:

//Init.cpp file
//render the world
bool Init::render_world(TTF_Font* fontTypes, SDL_Texture *image, SDL_Renderer* render)
{
   
   //to count the frame rate
   Timer fps;

   //to cap the frame rate
   Timer capFps;

   //load the textures
   ImageManager load;

   //the stream for the timer
   std::stringstream fpsFont;

   //draw the world
   World world;

   //the timer for the fps
   int frame = 0;

   //the x and y position of the arrow selection sprite
   int x = 315, y = 280;

   //givee the font color
   SDL_Color fontColor = { 255, 0, 0 };

   std::string choices[] = { "Fargo Game Engine", "Start engine", "Exit engine" };

   //boolean variable to accept user input for quit, start, and menu.
   bool quit = false;
   bool menu = true;
   bool start = false;

   fps.startClock();

   //Main Loop:
   while (!quit)
   {
      //handle events and menu selector!
      while (SDL_PollEvent(&event))
      {
         //user request quit
         if (event.type == SDL_QUIT || event.key.keysym.sym == SDLK_ESCAPE)
         {
            quit = true;
         }

         switch (menu)
         {
            case true:
               if (event.type == SDL_KEYDOWN)
               {
                  if (event.key.keysym.sym == SDLK_w)
                  {
                     render_texture(x = 315, y = 280, image, render, &sprites.at(arrow));
                  }
                  else if (event.key.keysym.sym == SDLK_s)
                  {
                     render_texture(x = 315, y = 310, image, render, &sprites.at(arrow));
                  }
                  else if (event.key.keysym.sym == SDLK_RETURN)
                  {
                     if (x == 315 && y == 280)
                     {
                        start = true;
                        menu = false;
                     }
                     else if (x == 315 && y == 310)
                     {
                        quit = true;
                     }
                  }
               }
               break;
            case false:
               world.player_input(event);
               break;
         }
      }
   
      //Calculate and correct fps
      float avgFPS = frame / (fps.fetch_ticks() / 1000.f);
      if (avgFPS > 2000000)
      {
         avgFPS = 0;
      }

      fpsFont.str("");

      fpsFont << "FPS:" << avgFPS;

      //this will load the fps font images
      fpsText = load.load_font(fpsFont.str().c_str(), fontTypes, fontColor, render);

      SDL_RenderClear(render);

      if (menu == true)
      {
         //the arrow selector texture being loaded
         render_texture(x, y, image, render, &sprites.at(arrow));

         //render the text of choices
         render_texture(340, 250, load.load_font(choices[0], fontTypes, fontColor, render), render, NULL);
         render_texture(340, 280, load.load_font(choices[1], fontTypes, fontColor, render), render, NULL);
         render_texture(340, 310, load.load_font(choices[2], fontTypes, fontColor, render), render, NULL);
      }
      else if (start == true)
      {
         //render the world
         world.load_tiles(image, render, sprites);
      }

      //render the fps font
      render_texture(10, 580, fpsText, render, NULL);
      
      SDL_RenderPresent(render);

      frame++;

      //If frame finished early
      int frameTicks = capFps.fetch_ticks();
      if (frameTicks < fps.screenTick)
      {
         //Wait remaining time
         SDL_Delay(fps.screenTick - frameTicks);
      }
   }

   close(fontTypes, image, render);

   return quit;
}


Please, help me understand why is this happening. I think it can be a memory leak but the pointers are private and being reused.
Naith


Joined: 03 Jul 2014
Posts: 158
Sorry to say it, but I am really unsure about the structure of your code.

Does your render_world function execute each frame?

Why are you having the main loop (' while(!quit) ') in the render_world function? In my opinion you should have it the other way around, i.e, have a main loop which execute an update function (which calculates the FPS, checks for events and such) and after that, the render_world function is called.

It looks to me that you're loading some font file (' fpsText = load.load_font(fpsFont.str().c_str(), fontTypes, fontColor, render); '), each frame. You really shouldn't do that. Instead, load the textures, fonts and such in some Create/Init function before the main loop start and destroy everything in your Close function.

Also, by the text written out in the console, it looks like you're calling some CreateTexture function somewhere in your code and you should show us that aswell.
cyberspace009


Joined: 11 Jun 2015
Posts: 3
Location: Orlando, FL
Quote:
Why are you having the main loop (' while(!quit) ') in the render_world function? In my opinion you should have it the other way around, i.e, have a main loop which execute an update function (which calculates the FPS, checks for events and such) and after that, the render_world function is called.


Hey, Naith

Thank you for responding my post and providing some insight. I will rename the "render_world(...)" function into "main_loop(...)" or something. Sorry to cause any confusion Very Happy .

Quote:
It looks to me that you're loading some font file (' fpsText = load.load_font(fpsFont.str().c_str(), fontTypes, fontColor, render); '), each frame. You really shouldn't do that.


I am glad you mention the issue about the [b]fpsText[b] I will fix that and see if it improves.

Quote:
Also, by the text written out in the console, it looks like you're calling some CreateTexture function somewhere in your code and you should show us that aswell.


I never created such a function, CreateTexture, so I will look into my code and see what's going on.

Thanks for the help![/quote]
Solved!
cyberspace009


Joined: 11 Jun 2015
Posts: 3
Location: Orlando, FL
I found the solution to the problem:

Naith was right about this:

Quote:
It looks to me that you're loading some font file (' fpsText = load.load_font(fpsFont.str().c_str(), fontTypes, fontColor, render); '), each frame. You really shouldn't do that. Instead, load the textures, fonts and such in some Create/Init function before the main loop start and destroy everything in your Close function.


My game is able to stay idle for sometime now. I can finally get back to work on my project.

Thank you very much, Naith!