简体   繁体   中英

C++ program memory usage keeps increasing when using SDL_TTF

I am doing my code for a simple game using SDL library. I have 2 questions regarding this:

  1. If I don't put a breakpoint and let the program runs by itself, the memory usage going high (like 20-30x compared to the beginning). However, if I stop the loop (main loop of the game) and click by myself, the memory usage stay same after around 100 clicks(?). Why?

  2. So I decided to use instrument to analyze the reason why since I think in the loop I might adding new element without destroying it. And everytime I hit record in Instrument, the app goes on for 5 seconds and shutdowns with a crash report (The app runs perfectly, with the memory stuff, in xcode)

    Thread 0 Crashed:: Dispatch queue: com.apple.main-thread

    0 org.libsdl.SDL2 0x0000000100f5bea9 0x100ee9000 + 470697

    1 Spike 10 0x0000000100ea40cc imageTexture::render() + 50 (imageTexture.cpp:37)

    2 Spike 10 0x0000000100ea2b70 GUI::renderImage() + 40 (GUI.cpp:140)

    3 Spike 10 0x0000000100ea30b2 GUI::run() + 1258 (GUI.cpp:172)

    4 Spike 10 0x0000000100ea439e main + 36 (main.cpp:15)

    5 libdyld.dylib 0x00007fff8c60f5ad start + 1

These are some codes that I think might related:

For number 3: //imageVector is just a vector contain all the imageVector pointer

SDL_RenderClear( gRenderer );
for (int i = 0; i < imageVector.size(); i++) {
    imageVector[i]->render();
}

For number 2:

//set rendering space and render to screen
SDL_Rect temp = {x, y, width, height};
//render to the screen
SDL_RenderCopy(gRenderer, texture, NULL, &temp);

After a long try disable/enable code around to find what happen, I still have no idea what's wrong.

This is my current loop:

while (true) {
    textVector[0]->setInput(system->output());
    renderImage();
    renderText();
    SDL_RenderPresent( gRenderer );
}

Some code for it:

void textTexture::setInput(std::string newText) {
    gText = newText;
}

void GUI::renderImage() {
    SDL_RenderClear( gRenderer );
    for (int i = 0; i < imageVector.size(); i++) {
        imageVector[i]->render();
    }
}

void GUI::renderText() {
    for (int i = 0; i < textVector.size(); i++) {
        textVector[i]->render();
    }
}

void textTexture::render() {
    //set rendering space and render to screen
    SDL_Rect temp = {x, y, width, height};

    //recreate the texture
    createTextureFromRenderedText(gText);

    //render to the screen
    SDL_RenderCopy(gRenderer, texture, NULL, &temp);
}

void textTexture::createTextureFromRenderedText(std::string text) {
    if (text != "") {
        SDL_Surface* textSurface = TTF_RenderText_Blended_Wrapped(gFont, text.c_str(), textColor, 600);

        if( textSurface == NULL )
        {
            printf( "Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError() );
        }
        else
        {
            //Create texture from surface pixels
            texture = SDL_CreateTextureFromSurface( gRenderer, textSurface );
            if( texture == NULL )
            {
                printf( "Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError() );
            }
            else
            {   
                //Get image dimensions
                width = textSurface->w;
                height = textSurface->h;
            }

            //Get rid of old surface
            SDL_FreeSurface( textSurface );
        }
    }
}

Find the problem. I copy the style of lazyfoo and try to implement in mine so when I render new textTexture, it actually create new texture without destroying it. By using SDL_DestroyTexture, everything works fine

Found the answer here: https://stackoverflow.com/questions/24938695/memory-leak-sdl-while-using-sdl-createtexturefromsurface

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM