3

I was making a game with SDL2, and shortly in the game was taking too much RAM to even close. I stripped down the code, and found that even this pointless, one file program uses 100% of my CPU and a gig of RAM.

#include "SDL.h"
#include "SDL_image.h"
#include <cassert>

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

SDL_Renderer* gRenderer;
SDL_Window* gWindow;

void init();
void close();

int main()
{
    init();
    SDL_Event e;
    bool quit;

    while( !quit )
    {
         while( SDL_PollEvent( &e ) != 0 )
         {
              if( e.type == SDL_QUIT )
                  quit = true;
         }
    }
    close();
}

void init()
{
    assert( SDL_Init( SDL_INIT_EVERYTHING ) >= 0 );
    gWindow = SDL_CreateWindow( "Space", SDL_WINDOWPOS_UNDEFINED,
                   SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH,
                   SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
    assert( gWindow );

    gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED );
    assert( gRenderer );

    int imgFlags = IMG_INIT_PNG;
    assert( IMG_Init( imgFlags ) & imgFlags );

    SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
}

void close()
{
    SDL_DestroyRenderer( gRenderer );
    SDL_DestroyWindow( gWindow );

    IMG_Quit();
    SDL_Quit();
}
cp420
  • 85
  • 5
  • This is the recommended way to use SDL. – cp420 Jul 07 '16 at 02:55
  • 1
    What exactly is happening that you do not expect? Don't just list everything that you observe: say what you *expect* to have happened, then ask why it does not happen the way you expect. **There is no question in the above "question"** – Yakk - Adam Nevraumont Jul 07 '16 at 03:07
  • 1
    Okay, this program does literally nothing besides start SDL and loop, yet it takes 100% of my CPU and over a gigabyte of RAM. Adding a bit more to it makes the program halt entirely. I tested it on two computers, one with an i7 processor. This simple program should not have this impact and I want to know why it does. – cp420 Jul 07 '16 at 03:22
  • note: the program also starts the renderer. it does not have this problem without the renderer working. – cp420 Jul 07 '16 at 03:30
  • 1
    Related: [SDL_PollEvent vs SDL_WaitEvent](http://stackoverflow.com/q/18860243/183120). Once you start doing something meaningful, such as rendering a scene, you should get numbers reflective of reality. Now you just have a _hello, world_ setup, which isn't heavy enough to show you the real numbers. – legends2k Jul 07 '16 at 09:51

3 Answers3

2

You're running to program as fast as possible, therefore it's using all resources it can get.

Try limiting the FPS.

Ivan Rubinson
  • 3,001
  • 4
  • 19
  • 48
  • 1
    Yeah, tested it and it was running over 410,000 frames per second lol. I hard capped it at 60 and it fixed the issue. Thank you and user3684240 for the help. – cp420 Jul 08 '16 at 19:23
  • I've heard Starcraft 2 had the same bug in menues. Not related to SDL though. – Ivan Rubinson Jul 08 '16 at 21:25
2

As Ivan already mentioned, you should try limiting the FPS of your program. One way to do this is to use SDL_GetTicks to measure the duration and then use SDL_Delay if it is fast enough. That way, a modern computer will sleep most of the time while a bad one will still get good framerates:

while(running) {
  auto time = SDL_GetTicks();
  // Rendering and event handling
  if ((SDL_GetTicks() - time) < 10) {
    SDL_Delay(10);
  }
}

You should play a little with the numbers (10 and 10 in the example) in order to see how many FPS your program needs.

user3684240
  • 1,420
  • 2
  • 12
  • 18
0

Using assert can use a LOT of RAM, maybe you should consider not using these. Try just a simple if to test your initializes.

Edit: It's seem that the RAM problem didn't came from that. Sorry for the bad answer

Paingouin
  • 11
  • 3
  • 1
    This sounds very dubious as best. Do you have references ? – Quentin Jul 07 '16 at 10:08
  • I don't have references but I used to use assert() to check the initializes my SDL2 training [game](https://github.com/Paingouin/Roguelike-SDL2-train) and it take a lot of ram to run it (~380mo)... I have replaced the assert() by simple "if == NULL" conditions (I know it's not totally error checking but it works for most of major issues) and my prog take now 7.8mo "only"... If someone can explain why... – Paingouin Jul 07 '16 at 11:12
  • I've retry to re-use assert and my prog work correctly (no more 380mo)... I can't explain why... Sorry for the bad answer. I though it was that. – Paingouin Jul 07 '16 at 11:22