0

I am in the progress of making a game which uses SDL_TTF and SDL_Image. I have one function called browseinventory which allows you to check out the stats of an item once you hover over it. That's where the memory leak occurs. Baically once you've hovered over an item slot the program check if there's anything in said slot and if there is, Text class objects are set with the item's parameters and then rendered.
I am pretty sure that the fault lies within the Text class, as memory usage rapidly increases once I've hovered over an item.

Text class:

class Text
{
public:
    Text() {w = 0; h = 0; saying = ""; Textthing = NULL;}
    ~Text(){destroy();}
    void destroy(){SDL_DestroyTexture(Textthing); Textthing = 0; saying = "", w = 0, h = 0;}
    void render(int, int, int, SDL_Renderer*);
void settext(string, SDL_Color);
void rendertest(int, int, SDL_Renderer*);
int rows;

private:
    string saying;
    SDL_Texture *Textthing;
    int w, h;
    unsigned int flag1 = 0, flag2, loops = 0;
    string saying1;
    unsigned int counter;
    bool spaceflag = 0;
};  

render():

void Text::render(int x, int y, int maxchars = 100, SDL_Renderer *Temp = Saviour)
{
    rows = 0;
    flag1 = 0, flag2 = maxchars, loops = 0;
    int increment = maxchars;
    if (resolution == "800x600")
        {
            flag2 = maxchars*0.8;
            increment = maxchars*0.8;
        }
    spaceflag = 0;
    do
{
    spaceflag = 0;
    for (counter = flag1; counter < flag2 && counter < saying.length(); counter++)
        {
            if (counter > flag2-10 && saying[counter] == ' ')
                {
                    spaceflag = 1;
                    break;
                }
            saying1 += saying[counter];
        }

    asdfgfa1.settext(saying1);
    asdfgfa1.rendertest(x, y + loops*(SCREEN_HEIGHT/19), Temp);
    saying1 = "";
    loops++;
    rows++;
    if (flag2 > saying.length())
        break;
    if (spaceflag == 0)
    {
        flag1 = flag2;
        flag2 += increment;
    }
    else
    {
        flag1 = counter;
        flag2 = counter + increment;
    }
}while (1);
asdfgfa1.destroy();
SDL_DestroyTexture(Textthing);
Textthing = 0;

}

setText():

void Text::settext(string stuff, SDL_Color Colour = {255, 255, 255})
{
    saying = stuff;
    SDL_Surface *Surf = TTF_RenderText_Solid(Arial, stuff.c_str(), Colour);
    w = Surf->w;
    h = Surf->h;
    Textthing = SDL_CreateTextureFromSurface(Saviour, Surf);
    SDL_FreeSurface(Surf);
    Surf = 0;
}
roschach
  • 8,390
  • 14
  • 74
  • 124
Hirako837
  • 9
  • 1
  • Do you call `settext` multiple times without destroying the texture in between? And how do you know there's a memory leak? – Some programmer dude Sep 06 '15 at 10:24
  • Did you consider to run your code under a memory leak detector like valgrind? – πάντα ῥεῖ Sep 06 '15 at 10:25
  • @JoachimPileborg I have multiple Text objects and I call settext around 15 times, for each item parameter. However I destroy them at the end of my loop. Is it possible that they still eat up my memory? – Hirako837 Sep 06 '15 at 10:43
  • What I mean is, do you call `settext` *on the same object* multiple times without destroying the texture in between? And, *how* do you know you have a memory leak? – Some programmer dude Sep 06 '15 at 11:16
  • I don't think I do call it on the same object multiple times, give me a while to go through my code and check. I know that I have simply because I used task manager and check RAM usage at the beginning of the program and after browsing inventory – Hirako837 Sep 06 '15 at 11:19
  • You might want to read [this old SO question and its answer](http://stackoverflow.com/q/5734066/440558) before using the task manager to detect memory leaks. Also, some operating systems will let memory you free in a process still be mapped to the process, in case the process needs to allocate it again, this can often be mistaken for a leak but it isn't. Try to find some other tools to see if you really have some leaks. – Some programmer dude Sep 06 '15 at 11:43

0 Answers0