Help, game is killing my computer

Started by
10 comments, last by sander242 14 years, 11 months ago
I finally finished my game with SDL. It's everything i hoped for. But when i started playing i noticed that my computer was getting slower and slower. i finally checked the task manager and my game already used over 50000 kb. but when i started my game it used 6000kb. the usage goes up by 1k every time i go on a another page like my shop page, or my explore page. I have no idea what to do maybe you can help me. I think my game keeps using more memory all the time because i use the SDL_FillRect() to empty my screens, I never actually empty the screen i just paint more and more pages over it. maybe you can help me. thx in advance.
Advertisement
Ya you definitely have a memory leak somewhere. Without code though it would be really hard to help. Are you freeing surfaces at all in your code?
Are you dynamically allocating memory in your game (using 'new', 'new []', 'malloc', etc.)? Did you ensure that you're cleaning it up once you don't need it anymore ('delete', 'delete []', 'free', etc.)?

Or perhaps you're loading images while they're already loaded - without unloading the old ones first (which is better solved by just using the already loaded images)?


Either way, can you show the code for the shop and explore page?
Create-ivity - a game development blog Mouseover for more information.
ok here's some source fom my game:
bool shop(){  int checkx, checky;  load_user();  load_shop();  uuenda();  bool lahku = false;  while(lahku == false)  {              if(SDL_WaitEvent(&event))              {                                   if(event.type == SDL_MOUSEBUTTONDOWN)                                   {                                                 if(SDL_GetMouseState(&checkx, &checky))                                                 {                                                 if((checkx > 364)&&(checkx < 549)&&(checky > 101)&&(checky < 134))                                                 {upgrade(); SDL_FillRect(screen,NULL,0); shop();}                                                 if((checkx > 79)&&(checkx < 264)&&(checky > 264)&&(checky < 297))                                                 {heal(); SDL_FillRect(screen,NULL,0);shop();}                                                 if((checkx > 79)&&(checkx < 264)&&(checky > 328)&&(checky < 361))                                                 {charge(); SDL_FillRect(screen, NULL, 0); shop();}                                                 if((checkx > 234)&&(checkx < 419)&&(checky > 411)&&(checky < 444))                                                 {lahku = true;}                                                 }                                    }                                   if(event.type == SDL_QUIT)                                   {clean_up(); TTF_CloseFont(font); TTF_Quit(); SDL_Quit(); return 0;}              }  }}       switch (quit)    {    case 1:         clean_up();          TTF_CloseFont(font);          TTF_Quit();          SDL_Quit();          return 0;         break;    case 2:         SDL_FillRect(screen,NULL,0);         explore();         SDL_FillRect(screen,NULL,0);         goto LOOP;         break;    case 3:         SDL_FillRect(screen,NULL,0);         shop();         SDL_FillRect(screen,NULL,0);         goto LOOP;         break;    case 5:         clean_up();          TTF_CloseFont(font);          TTF_Quit();          SDL_Quit();          return 0;         break;    }    }
load_user and load_shop are probably the culprits. There isn't any immediate evidence of any unloading, and you recursively call shop so they just keep getting loaded again, and again, and again.
Nothing problematic looking in there. Show us some more. Specifically, show us any place where you use a new or delete or anywhere you work with SDL_Surface's (excluding the screen's surface). I'd hazard a guess that it's a surface problem, since some of the SDL functions (SDL_DisplayFormat is a common one) return copies rather than modifying the surface.

Edit: @popsoftheyear: Oh, wow I had completely missed the recursive shop calls. Guess that's what happens when there's three expressions crammed into one line. :/
Quote:Original post by Ezbez
Edit: @popsoftheyear: Oh, wow I had completely missed the recursive shop calls. Guess that's what happens when there's three expressions crammed into one line. :/


Yeah in a time of people having trouble with employment, I'm incredibly blessed to have a job (where the millionish lines of code can be at times far less well written than the code posted here).

This is a good point though sander, it would be beneficial (not just for others but for you as well) to try not to have multiple statements in a line. :)
OK, I tried to repair it myself with several tries. none worked :D. Anyhow its my first game so i want it to work, so here's the whole source code. You don't have to rad it all just look at the usual memory leak spots. maybe you can find the memory leak. plz don't mind the small scripting inefficencies, im just learning. thx in advance:
#include <SDL/SDL.h>#include <SDL/SDL_ttf.h>#include <SDL/SDL_image.h>#include <string>#include <iostream>#include <sstream>#include <fstream>using namespace std;const int SCREEN_WIDTH = 640;const int SCREEN_HEIGHT = 480;const int SCREEN_BPP = 32;int HP, power, enemyHP, lvl, damage, shield,  weapon, engines, money;SDL_Surface *screen = NULL;SDL_Surface *laev = NULL;SDL_Surface *elud = NULL;SDL_Surface *energia = NULL;SDL_Surface *vastane = NULL;SDL_Surface *kilbid = NULL;SDL_Surface *relvad = NULL;SDL_Surface *mootor = NULL;SDL_Surface *img1 = NULL;SDL_Surface *img2 = NULL;SDL_Surface *img3 = NULL;SDL_Surface *img4 = NULL;SDL_Surface *img5 = NULL;SDL_Surface *text = NULL;SDL_Surface *text2 = NULL;SDL_Surface *text3 = NULL;SDL_Event event;TTF_Font *font = NULL;SDL_Color textColor = {0,0,0};SDL_Color textColor2 = {255,255,255};SDL_Surface *load_image(string filename){            SDL_Surface *loadedImage = NULL;            SDL_Surface *optimizedImage = NULL;            loadedImage = IMG_Load(filename.c_str());            if (loadedImage != NULL)            {                            optimizedImage = SDL_DisplayFormat(loadedImage);                            SDL_FreeSurface(loadedImage);                            if( optimizedImage != NULL )                            {                            Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 120, 233, 230 );                            SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );                            }            }            return optimizedImage;}void apply_surface(int x, int y, SDL_Surface *source,SDL_Surface *destination){     SDL_Rect offset;     offset.x = x;     offset.y = y;     SDL_BlitSurface(source, NULL, destination, &offset);}bool init(){   if(SDL_Init(SDL_INIT_EVERYTHING) == -1)     {return false;}               if (TTF_Init() == -1)     {return false;}               screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);     if (screen == NULL)     {return false;}          SDL_WM_SetCaption("Space Game 1.00",NULL);    return true;}bool load_files0(){ font = TTF_OpenFont( "system/arial.ttf", 25);  if(font == NULL) {return false;} return true;}  bool load_Mainmenu(){     img1 = load_image("image/backround2.png");      img2 = load_image("image/title.png");     img3 = load_image("image/box.png");     text = TTF_RenderText_Solid(font, "START", textColor);     text2 = TTF_RenderText_Solid(font, "QUIT", textColor);     apply_surface(0,0,img1,screen);     apply_surface(147,53,img2,screen);     apply_surface(230,361,img3,screen);     apply_surface(230,236,img3,screen);     apply_surface(286,240,text,screen);     apply_surface(296,365,text2,screen);         return true;}bool load_center(){     img1 = load_image("image/backround1.png");     img2 = load_image("image/title.png");     img3 = load_image("image/box.png");      img4 = TTF_RenderText_Solid(font, "Explore", textColor);     img5 = TTF_RenderText_Solid(font, "Shop", textColor);     text2 = TTF_RenderText_Solid(font, "Quit", textColor);     apply_surface(0,0,img1,screen);     apply_surface(157,53,img2,screen);      apply_surface(240,222,img3,screen);     apply_surface(240,279,img3,screen);     apply_surface(240,391,img3,screen);      apply_surface(292,226,img4,screen);     apply_surface(304,283,img5,screen);     apply_surface(304,395,text2,screen);          }bool load_victory(){     img1 = load_image("image/backround3.png");     img2 = load_image("image/box.png");     img3 = TTF_RenderText_Solid(font, "Continue", textColor);     img4 = TTF_RenderText_Solid(font, "VICTORY", textColor);     img5 = TTF_RenderText_Solid(font, "You have been awarded 150 credits", textColor2);          apply_surface(0,0,img1,screen); apply_surface(225,134,img2,screen);     apply_surface(225,327,img2,screen); apply_surface(166,219,img5,screen);     apply_surface(230,137,img4,screen); apply_surface(230,330,img3,screen);          SDL_Flip(screen);}bool load_loss(){  img1 = load_image("image/backround3.png");  img2 = load_image("image/box.png");  img3 = TTF_RenderText_Solid(font, "Continue", textColor);  img4 = TTF_RenderText_Solid(font, "You, lost", textColor);  img5 = TTF_RenderText_Solid(font, "Repairs will be done at the shop", textColor2);       apply_surface(0,0,img1,screen); apply_surface(225,134,img2,screen);  apply_surface(225,327,img2,screen); apply_surface(166,219,img5,screen);  apply_surface(230,137,img4,screen); apply_surface(230,330,img3,screen);       SDL_Flip(screen);   }bool load_explore(){                    img1 = load_image("image/backround3.png");              img2 = load_image("image/box.png");              img3 = TTF_RenderText_Solid(font, "Powersource", textColor);              img4 = TTF_RenderText_Solid(font, "Shields", textColor);              img5 = TTF_RenderText_Solid(font, "Weapons", textColor);              text = TTF_RenderText_Solid(font, "Engine", textColor);              text2 = TTF_RenderText_Solid(font, "Attack", textColor);              text3 = TTF_RenderText_Solid(font, "Flee", textColor);                            apply_surface(0,0,img1, screen); apply_surface(50,20,laev,screen);              apply_surface(362,20,laev,screen);apply_surface(60,245,img2, screen);               apply_surface(60,286,img2, screen);apply_surface(60,327,img2, screen);              apply_surface(60,368,img2, screen);apply_surface(141,422,img2, screen);               apply_surface(359,422,img2, screen);apply_surface(372,245,img2, screen);               apply_surface(372,286,img2, screen);apply_surface(372,327,img2, screen);               apply_surface(372,368,img2, screen); apply_surface(65,248,img3, screen);               apply_surface(65,289,img4, screen); apply_surface(65,330,img5, screen);               apply_surface(65,371,text, screen); apply_surface(364,425,text2, screen);               apply_surface(146,425,text3, screen);                            SDL_Flip(screen);              return true;}bool load_shop(){    img1 = load_image("image/backround1.png");   img2 = load_image("image/box.png");   img3 = TTF_RenderText_Solid(font, "Click to heal", textColor);   img4 = TTF_RenderText_Solid(font, "Click to recharge", textColor);   img5 = TTF_RenderText_Solid(font, "Upgrade ship", textColor);   text = TTF_RenderText_Solid(font, "Main Menu", textColor);      apply_surface(0,0,img1,screen); apply_surface(63,41,laev,screen);    apply_surface(234,411,img2,screen); apply_surface(364,104,img2,screen);   apply_surface(79,264,img2,screen); apply_surface(79,328,img2,screen);   apply_surface(315,264,img2,screen); apply_surface(315,328,img2,screen);   apply_surface(369,103,img5,screen); apply_surface(84,267,img3,screen);   apply_surface(84,331,img4,screen); apply_surface(239,413,text,screen);   SDL_Flip(screen);   }bool load_user(){     ifstream health("system/user/HP.txt");     health >> HP;     health.close();     ifstream level("system/user/lvl.txt");     level >> lvl;     level.close();     ifstream powers("system/user/power.txt");     powers >> power;     powers.close();            switch (lvl)              {              case 1:              laev = load_image("image/ship1.png");              break;              case 2:              laev = load_image("image/ship2.png");              break;              case 3:              laev = load_image("image/ship3.png");              break;              case 4:              laev = load_image("image/ship4.png");              break;              case 5:              laev = load_image("image/ship5.png");              break;              }     }bool load_enemy(){            damage = (lvl) * 150;            enemyHP = (lvl) * 3000;}                   bool MainMenu(){    int checkx;    int checky;    int quit = 0;                if( load_Mainmenu() == false )    {return false;}        if(SDL_Flip(screen) == -1)    {return 1;}        while(quit == 0)    {      if(SDL_WaitEvent(&event))      {            if(event.type == SDL_MOUSEBUTTONDOWN)            {                 if(SDL_GetMouseState(&checkx, &checky))                 {                        if((checkx > 230) && (checkx < 415) && (checky > 361) && (checky < 394))                       {quit = 1;}                       if ((checkx > 230) && (checkx < 415) && (checky > 236) && (checky < 269))                       {quit = 2;}                 }            }            if(event.type == SDL_QUIT)            {quit = 1;}       }    }    switch (quit)    {           case 1:           {                    SDL_FreeSurface(img1); SDL_FreeSurface(img2);                SDL_FreeSurface(img3); SDL_FreeSurface(text);                SDL_FreeSurface(text2); TTF_CloseFont(font);                 TTF_Quit(); SDL_Quit(); return 0;           }           case 2:                {                             SDL_FillRect(screen,NULL,0);                             return true;                }    }}int shields(){ bool nameEntered = false;         string shi = "";         SDL_EnableUNICODE( SDL_ENABLE );         while(nameEntered == false)         {                           string temp = shi;                           if(SDL_WaitEvent(&event))                           {                                             if(event.type == SDL_KEYDOWN)                                                 {                                                    if( (event.key.keysym.unicode >= (Uint16) '0' ) && (event.key.keysym.unicode <= (Uint16)'9' ))                                                    {                                                    shi += (char)event.key.keysym.unicode;                                                    }                                                 }                                             if( event.key.keysym.sym == SDLK_BACKSPACE )                                                 {                                                 shi.erase(shi.length() - 1);                                                 SDL_FreeSurface(kilbid);                                                 kilbid = TTF_RenderText_Solid(font, shi.c_str(), textColor);                                                 apply_surface(430,289,kilbid,screen);                                                 SDL_Flip(screen);                                                 }                                             if( shi != temp )                                                  {                                                  SDL_FreeSurface(kilbid);                                                  kilbid = TTF_RenderText_Solid( font, shi.c_str(), textColor );                                                  apply_surface(430,289,kilbid,screen);                                                  SDL_Flip(screen);                                                  }                                              if(( event.type == SDL_KEYDOWN ) && ( event.key.keysym.sym == SDLK_RETURN ) )                                                  {                                                  nameEntered = true;                                                  }                          }                                 }         stringstream stream;         stream << shi;         stream >> shield;   }int weapons(){ bool nameEntered = false;         string weap = "";         SDL_EnableUNICODE( SDL_ENABLE );         while(nameEntered == false)         {                           string temp = weap;                          if(SDL_WaitEvent(&event))                           {                                             if(event.type == SDL_KEYDOWN)                                                 {                                                    if( (event.key.keysym.unicode >= (Uint16) '0' ) && (event.key.keysym.unicode <= (Uint16)'9' ))                                                    {                                                    weap += (char)event.key.keysym.unicode;                                                    }                                                 }                                             if((event.key.keysym.sym == SDLK_BACKSPACE )&&(weap.length() != 0))                                                 {                                                 weap.erase(weap.length() - 1);                                                 SDL_FreeSurface(relvad);                                                 relvad = TTF_RenderText_Solid(font, weap.c_str(), textColor);                                                 apply_surface(430,330,relvad,screen);                                                 SDL_Flip(screen);                                                 }                                             if( weap != temp )                                                  {                                                  SDL_FreeSurface(relvad);                                                  relvad = TTF_RenderText_Solid( font, weap.c_str(), textColor );                                                  apply_surface(430,330,relvad,screen);                                                  SDL_Flip(screen);                                                  }                                              if(( event.type == SDL_KEYDOWN ) && ( event.key.keysym.sym == SDLK_RETURN ) )                                                  {                                                  nameEntered = true;                                                  }                          }                                 }         stringstream stream;         stream << weap;         stream >> weapon;   }int engine(){  bool nameEntered = false;         string eng = "";         SDL_EnableUNICODE( SDL_ENABLE );         while(nameEntered == false)         {                           string temp = eng;                           if(SDL_WaitEvent(&event))                           {                                             if(event.type == SDL_KEYDOWN)                                                 {                                                    if( (event.key.keysym.unicode >= (Uint16) '0' ) && (event.key.keysym.unicode <= (Uint16)'9' ))                                                    {                                                    eng += (char)event.key.keysym.unicode;                                                    }                                                 }                                             if((event.key.keysym.sym == SDLK_BACKSPACE ) && (eng.length() !=  0))                                                 {                                                 eng.erase(eng.length() - 1);                                                 SDL_FreeSurface(mootor);                                                 mootor = TTF_RenderText_Solid(font, eng.c_str(), textColor);                                                 apply_surface(430,371,mootor,screen);                                                 SDL_Flip(screen);                                                 }                                             if( eng != temp )                                                  {                                                  SDL_FreeSurface(mootor);                                                  mootor = TTF_RenderText_Solid( font, eng.c_str(), textColor );                                                  apply_surface(430,371,mootor,screen);                                                  SDL_Flip(screen);                                                  }                                              if(( event.type == SDL_KEYDOWN ) && ( event.key.keysym.sym == SDLK_RETURN ) )                                                  {                                                  nameEntered = true;                                                  }                          }                                 }         stringstream stream;         stream << eng;         stream >> engines;       }bool renew(){     if(elud != NULL)     {             SDL_FreeSurface(elud);     }     if(energia != NULL)     {                SDL_FreeSurface(energia);     }     string elu = "", oud = "";     stringstream stream4,stream5;     stream4 << HP; stream4 >> elu;     stream5 << power; stream5 >> oud;     elud = TTF_RenderText_Solid(font, elu.c_str(),textColor);     energia = TTF_RenderText_Solid(font, oud.c_str(),textColor);     apply_surface(320,267,elud,screen); apply_surface(320,331,energia,screen);     SDL_Flip(screen); }bool update(){     if(elud != NULL)     {             SDL_FreeSurface(elud);     }     if(energia != NULL)     {                SDL_FreeSurface(energia);     }     if(vastane != NULL)     {                SDL_FreeSurface(vastane);     }     string heal = "", pow = "", vaenlane = "";     stringstream stream,stream2,stream3;     stream << HP; stream >> heal;     stream2 << power; stream2 >> pow;     stream3 << enemyHP; stream3 >> vaenlane;     elud = TTF_RenderText_Solid(font, heal.c_str(), textColor2);     energia = TTF_RenderText_Solid(font, pow.c_str(), textColor);     vastane = TTF_RenderText_Solid(font, vaenlane.c_str(), textColor2);     apply_surface(110,210,elud,screen); apply_surface(430,248,energia,screen);     apply_surface(430,210,vastane,screen);     SDL_Flip(screen);}bool fight(){     int checkx, checky;     update();     while((HP > 0) && (enemyHP > 0) && (power > 0))     {               if(SDL_WaitEvent(&event))                {                                   if(event.type == SDL_MOUSEBUTTONDOWN)                                   {                                                 if(SDL_GetMouseState(&checkx, &checky))                                                 {                                                      /*shields*/              if((checkx > 372)&&(checkx < 557) && (checky > 286)&&(checky < 319))                                                                               {shields();}                                                       /*weapons*/              if((checkx > 372)&&(checkx < 557) && (checky > 327)&&(checky < 360))                                                                               {weapons();}                                                      /*engines*/              if((checkx > 372)&&(checkx < 557) && (checky > 368)&&(checky < 401))                                                                               {engine();}                                                      /*attack*/               if((checkx > 359)&&(checkx < 554) && (checky > 422)&&(checky < 455))                                                                               {                                                                                          HP = HP - (damage - ((shield/2 * lvl) + (engines/25)));                                                                                          enemyHP = enemyHP - weapon;                                                                                          power = power - ((shield + weapon + engines)/5);                                                                                          SDL_FillRect(screen,NULL,0);                                                                                          load_explore();                                                                                          fight();                                                                               }                                                      /*flee*/                 if((checkx > 141)&&(checkx < 326) && (checky > 422)&&(checky < 455))                                                                               {return true;}                                                                     }                                                                                    }                                   if(event.type == SDL_QUIT)                                   {                                                  SDL_FreeSurface(laev); SDL_FreeSurface(img1);                                                 SDL_FreeSurface(img2); SDL_FreeSurface(img3);                                                 SDL_FreeSurface(img4); SDL_FreeSurface(img5);                                                 SDL_FreeSurface(text); SDL_FreeSurface(text2);                                                 SDL_FreeSurface(text3);                                                 TTF_CloseFont(font);                                                  TTF_Quit();                                                  SDL_Quit();                                                  return 0;                                   }                }        }        }bool victory(){     SDL_FillRect(screen,NULL,0);     load_victory();     int checkx, checky;     ofstream myfile;     myfile.open("system/user/HP.txt",ios::trunc);     myfile << HP;     myfile.close();     myfile.open("system/user/power.txt",ios::trunc);     myfile << power;     myfile.close();     ifstream other;     other.open("system/user/cash.txt");     other >> money;     other.close();     money = money + 150;     myfile.open("system/user/cash.txt",ios::trunc);     myfile << money;     myfile.close();     bool lahku = false;     while(lahku == false)     {         if(SDL_WaitEvent(&event))                {                                   if(event.type == SDL_MOUSEBUTTONDOWN)                                   {                                                 if(SDL_GetMouseState(&checkx, &checky))                                                 {                                                        if((checkx > 225)&&(checkx < 410) && (checky > 327)&&(checky < 360))                                                        {lahku = true;}                                                 }                                   }                                   if(event.type == SDL_QUIT)                                   {                                                  SDL_FreeSurface(img1); SDL_FreeSurface(img2);                                                 SDL_FreeSurface(img3); SDL_FreeSurface(img4);                                                 SDL_FreeSurface(img5);                                                 TTF_CloseFont(font);                                                  TTF_Quit();                                                  SDL_Quit();                                                  return 0;                                   }                }     }    }     bool loss(){  SDL_FillRect(screen, NULL,0);  load_loss();   int checkx, checky;  ofstream myfile;  myfile.open("system/user/HP.txt",ios::trunc);  myfile << HP;  myfile.close();  myfile.open("system/user/power.txt",ios::trunc);  myfile << power;  myfile.close();  bool lahku = false;  while(lahku == false)     {         if(SDL_WaitEvent(&event))                {                                   if(event.type == SDL_MOUSEBUTTONDOWN)                                   {                                                 if(SDL_GetMouseState(&checkx, &checky))                                                 {                                                        if((checkx > 225)&&(checkx < 410) && (checky > 327)&&(checky < 360))                                                        {lahku = true;}                                                 }                                   }                                   if(event.type == SDL_QUIT)                                   {                                                  SDL_FreeSurface(img1); SDL_FreeSurface(img2);                                                 SDL_FreeSurface(img3); SDL_FreeSurface(img4);                                                 SDL_FreeSurface(img5);                                                 TTF_CloseFont(font);                                                 TTF_Quit(); SDL_Quit();                                                 return 0;                                   }                }     }}       bool endfight(){     if(enemyHP <= 0)     {victory();}     if((HP <= 0) || (power <= 0))     {loss();}}bool explore(){     load_user();     load_enemy();     load_explore();     fight();     endfight();}     bool upgrade(){     ifstream myfile;     myfile.open("system/user/cash.txt");     myfile >> money;     myfile.close();     if(money - 1500 < 0)     {return true;}     else     {         lvl = lvl + 1;         money = money - 1500;         ofstream teine("system/user/lvl.txt",ios::trunc);         teine << lvl;         teine.close();         teine.open("system/user/cash.txt",ios::trunc);         teine << money;         teine.close();     }     switch(lvl)     {                case 1:                     HP = 1000;                     break;                case 2:                     HP = 2500;                     break;                case 3:                     HP = 4500;                     break;                case 4:                     HP = 7500;                     break;                case 5:                     HP = 9999;                     break;     }     ofstream other;     other.open("system/user/HP.txt", ios::trunc);     other << HP;     other.close();           switch(lvl)     {                case 1:                     power = 1000;                     break;                case 2:                     power = 2500;                     break;                case 3:                     power = 4500;                     break;                case 4:                     power = 7500;                     break;                case 5:                     power = 9999;                     break;     }     ofstream other2;     other2.open("system/user/power.txt", ios::trunc);     other2 << power;     other2.close();          uuenda();}bool heal(){     ifstream myfile;     myfile.open("system/user/lvl.txt");     myfile >> lvl;     myfile.close();     switch(lvl)     {                case 1:                     HP = 1000;                     break;                case 2:                     HP = 2500;                     break;                case 3:                     HP = 4500;                     break;                case 4:                     HP = 7500;                     break;                case 5:                     HP = 9999;                     break;     }     ofstream other;     other.open("system/user/HP.txt", ios::trunc);     other << HP;     other.close();     uuenda();}bool charge(){     ifstream myfile2;     myfile2.open("system/user/lvl.txt");     myfile2 >> lvl;     myfile2.close();     switch(lvl)     {                case 1:                     power = 1000;                     break;                case 2:                     power = 2500;                     break;                case 3:                     power = 4500;                     break;                case 4:                     power = 7500;                     break;                case 5:                     power = 9999;                     break;     }     ofstream other2;     other2.open("system/user/power.txt", ios::trunc);     other2 << power;     other2.close();     uuenda();}bool shop(){  int checkx, checky;  load_user();  load_shop();  renew();  bool lahku = false;  while(lahku == false)  {              if(SDL_WaitEvent(&event))              {                                   if(event.type == SDL_MOUSEBUTTONDOWN)                                   {                                                 if(SDL_GetMouseState(&checkx, &checky))                                                 {                                                 if((checkx > 364)&&(checkx < 549)&&(checky > 101)&&(checky < 134))                                                 {upgrade(); SDL_FillRect(screen,NULL,0); shop();}                                                 if((checkx > 79)&&(checkx < 264)&&(checky > 264)&&(checky < 297))                                                 {heal(); SDL_FillRect(screen,NULL,0);shop();}                                                 if((checkx > 79)&&(checkx < 264)&&(checky > 328)&&(checky < 361))                                                 {charge(); SDL_FillRect(screen, NULL, 0); shop();}                                                 if((checkx > 234)&&(checkx < 419)&&(checky > 411)&&(checky < 444))                                                 {lahku = true;}                                                 }                                    }                                   if(event.type == SDL_QUIT)                                   {                                                    SDL_FreeSurface(img1); SDL_FreeSurface(img2);                                                   SDL_FreeSurface(img3); SDL_FreeSurface(img4);                                                   SDL_FreeSurface(img5); SDL_FreeSurface(text);                                                   TTF_CloseFont(font);                                                    TTF_Quit();                                                    SDL_Quit();                                                    return 0;                                   }              }  }}            int main( int argc, char* args[] ){    if(init() == false )    {return 1;}    if(load_files0() == false )    {return 1;}        if(MainMenu() == false)    {return 1;}        LOOP:    if(load_center() == false)    {return 1;}    if(SDL_Flip(screen) == -1)    {return 1;}    int checkx, checky;    int quit = 0;    while (quit == 0)    {         if(SDL_WaitEvent(&event))               {                                   if(event.type == SDL_MOUSEBUTTONDOWN)                                   {                                                 if(SDL_GetMouseState(&checkx, &checky))                                                 {                                                  if((checkx > 240) && (checkx < 425) && (checky > 222) && (checky < 255))                                                 {quit = 2;}                                                 if ((checkx > 240) && (checkx < 425) && (checky > 279) && (checky < 312))                                                 {quit = 3;}                                                 if((checkx > 240) && (checkx < 425) && (checky > 391) && (checky < 424))                                                 {quit = 5;}                                                 }                                   }                                   if(event.type == SDL_QUIT)                                   {quit = 1;}               }    }    switch (quit)    {    case 1:         SDL_FreeSurface(img1); SDL_FreeSurface(img2);         SDL_FreeSurface(img3); SDL_FreeSurface(img4);         SDL_FreeSurface(img5); SDL_FreeSurface(text2);          TTF_CloseFont(font);          TTF_Quit();          SDL_Quit();          return 0;         break;    case 2:         SDL_FillRect(screen,NULL,0);         explore();         SDL_FillRect(screen,NULL,0);         goto LOOP;         break;    case 3:         SDL_FillRect(screen,NULL,0);         shop();         SDL_FillRect(screen,NULL,0);         goto LOOP;         break;    case 5:         SDL_FreeSurface(img1); SDL_FreeSurface(img2);         SDL_FreeSurface(img3); SDL_FreeSurface(img4);         SDL_FreeSurface(img5); SDL_FreeSurface(text2);         TTF_CloseFont(font);          TTF_Quit();          SDL_Quit();          return 0;         break;    }    }
+1 for one statement per line.
After quickly scanning your code, the problem seems to be what everyone else has already suggested; You're loading images into pointers and then never freeing them up again. I'll try to show what I mean in an example:

All of your images are cycling the same few pointers:
SDL_Surface *img1 = NULL;SDL_Surface *img2 = NULL;SDL_Surface *img3 = NULL;SDL_Surface *img4 = NULL;SDL_Surface *img5 = NULL;


And so each time you load a new area you're trying to overwrite the contents of these images. However in reality you're actually loading a new image and telling the pointer "okay, please point to the new image now". Which it does do successfully, but this has the problem that it leaves the old image still loaded in memory, but now with no pointer pointing at it, and so no way to access it!

Each time you move between zones you're repeating this and so you slowly build up more and more images in memory that you're no longer using, but can no longer access to remove them either. What you need to do is alter your code so that each time you try to load a new image with the same pointers, make sure you remove the old one first, or even use seperate pointers for all images.

// Check if "img1" is pointing to an image alreadyif (img1 != NULL){    // Free image out of memory    SDL_FreeSurface(img1);    // Set to NULL so we know it's okay to use    // and avoid accessing the now empty memory    // by accident    img1 = NULL;}


Now I'm not quite sure what the exact code is as I don't use SDL, but I saw that function in your code and I'm assuming it's the correct one. What you should try to do is; For every pointer you're about to use, that may already be pointing to something, check if that's the case. If the image is pointing to something, free it up first before loading something new, and it's as simple as that code above.

So go through each of your load functions and for each pointer you use, make sure it's okay to use it first. Alternatively you could build a seperate "Unload All" function that you can call each time that checks all image pointers you're using.

Just double-check that's the right way to release the images first, and correct the code if not. Other than that this should hopefully help you out!

As a general rule of thumb, as soon as you allocate something to a pointer in code, if necessary, write the code to de-allocate it straight away so you don't forget!

This topic is closed to new replies.

Advertisement