Memory constantly increasing even after freeing surfaces and textures in SDL2

403 Views Asked by At

I have a text object, which has a setup, draw and free. Just to test if my free function completely free's all of the memory being used, I created a loop that keeps setting it up and freeing it. As it does this, the memory usage of the program just keeps increasing at about 0.2mb/s, so it is clear that I am missing something. I can't find anything in the code that I have missed that should free memory usage. If anyone could help with figuring out what is going wrong thanks!

Here are the parts:

text.cpp This Load_Text_Textures loads a surface and creates a texture for the text and stuff around it


void Text::Load_Text_Textures(SDL_Renderer *renderer)
{
    
    textSurface = TTF_RenderText_Blended(fonts[fontIndex], text.c_str(), colour);     ///Recreate the textures/surfaces
        


    if (!textSurface){
        cout << "Unable to create surface of text " << text << " error is: " << SDL_GetError() << endl;
    }
    textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
    if (!textTexture){
        cout << "Unable to create texture from surface of text " << text << " error is: " << SDL_GetError() << endl;
    }

    SDL_FreeSurface(textSurface);
    textSurface = NULL;

    SDL_QueryTexture(textTexture, NULL, NULL, &textW, &textH);
    textRect = {x, y, textW, textH};

}


Where I set things up


void Text::Setup(SDL_Renderer *renderer, string txt, int xPos, int yPos, int txtSize, SDL_Color Colour, bool isBold, string fontType, bool isWrapped, int theWrapWidth, bool isClickable )
{
    if (setup == false){


        numOfInstances += 1;

        clickable = isClickable;
        wrapped = isWrapped;
        wrappedWidth = theWrapWidth;
        text = txt;
        textSize = txtSize;
        bold = isBold;
        colour = Colour;


        textW = 0;
        textH = 0;
        x = xPos;
        y = yPos;
        initialX = x;
        initialY = y;


        fontIndex = textSize-lowestFontSize -1;





        if (textTexture != NULL){
            SDL_DestroyTexture(textTexture);    ///Free memory not going to be used again.
            textTexture = NULL;
        }


        if (text != ""){    ///Only create textures if there is text


            ///Load consecutive widths and height of text for hovering over
            Load_Text_Widths(renderer);

            Load_Text_Textures(renderer);    ///Loads textures/surfaces/whatever


        }




        setup = true;



    }



    else{
        //cout << "Trying to setup a text already setup! " << text << endl;
    }


}

Where i free memory



void Text::Free_All()
{
    if (setup == true){


        if (textSurface == NULL){
            //cout << "Text surface already free'd" << endl;
        }
        else{
            SDL_FreeSurface(textSurface);
            textSurface = NULL;
            //cout << "Free'd surface \n";
        }

        if (textTexture == NULL){
            //cout << "Could not free memory for text \"" << text << "\". Error from SDL is: " << TTF_GetError() << endl;
        }
        else{
            SDL_DestroyTexture(textTexture);
            textTexture = NULL;
        }



        ///For TTF_Init();
        numOfInstances -= 1;



        //cout << "Free'd " << text << endl;

    }




    else{
        //cout << "Text not setup yet when trying to free!" << endl;
    }



    setup = false;


}

In the part for testing this is all i do


    counter += 1;

    if (counter % 2 == 0){
        testText.Setup(renderer, "Some Text", 300, 200, 60, {0,0,0}, false, "arial.ttf", false, 0, false);
    }
    else{
        testText.Free_All();
    }

    testText.Draw_Text(renderer);

So every loop i either free the text or set it up. It begins as setup btw, so adding 1 before it just means it will free it which it is already setup. I just cannot figure out where its not freeing memory not being used.

Edit:

void Text::Load_Text_Widths(SDL_Renderer *renderer)
{
    if (clickable == false){
        string currentChars;
        widthsOfCharacters.clear();
        for (int j=0; j < text.length(); j++){
            currentChars = "";
            for (int k=0; k <= j; k++){ ///make currentChars equal to the characters in "text" up to the index of j
                currentChars += text[k];
            }

            int w,h;

            if (bold == true){
                TTF_SizeText(fontsBold[fontIndex], currentChars.c_str(), &w, &h);
            }
            else{
                TTF_SizeText(fonts[fontIndex], currentChars.c_str(), &w, &h);
            }

            widthsOfCharacters.push_back(w);
            heightsOfCharacters.push_back(h);
        }
    }
}


0

There are 0 best solutions below