SDL_TTF странная утечка памяти - PullRequest
0 голосов
/ 15 сентября 2018

У меня какая-то странная утечка памяти с SDL_TTF. При многократном вызове функции изменения размера я вижу, что использование программы увеличивается в ОЗУ с 10 до 20 Мб, а затем остается там. Я могу изменить размер снова, сколько я хочу, но это никогда не выходит за рамки или обратно.

Вот мой текстовый класс:

#ifndef TEXT_H
#define TEXT_H

class Text
{
public:
    Text(){
        srcRect.x = 0;
        srcRect.y = 0;
        srcRect.w = 0;
        srcRect.h = 0;
        dstRect.x = 0;
        dstRect.y = 0;
        dstRect.w = 0;
        dstRect.h = 0;

        good = false;
        visable = true;

        sprite = NULL;
        font = NULL;

        ZeroMemory(text, sizeof(text));
        ZeroMemory(fontPath, sizeof(fontPath));
    }
    ~Text(){
        // Something
    }

    void SetFont(const char* fontPath, int fontSize){
        // Check if a font is already loaded
        if(font != NULL) TTF_CloseFont(font);

        // Reset the fontPath string
        if(strcmp(this->fontPath, fontPath)){
            ZeroMemory(this->fontPath, sizeof(this->fontPath));
            strcpy(this->fontPath, fontPath);
        }

        // Set the new fontSize
        this->fontSize = fontSize;

        // Load the new fontface and size
        font = TTF_OpenFont(fontPath, fontSize);

        // Check for errors
        if(font == NULL){
            cout << "Failed loading font: " << fontPath << endl;

            good = false;
            return;
        } else good = true;

        // Set the new font
        this->font = font;
    }

    void Initialize(int x, int y){
        this->dstRect.x = x;
        this->dstRect.y = y;
    }

    void Load(SDL_Renderer* renderer, const char* text, const char* fontPath, int fontSize, SDL_Color text_color){
        // Check if a texture is already loaded
        if(sprite != NULL) SDL_DestroyTexture(sprite);

        // Set the new text
        if(strcmp(text, this->text)){
            ZeroMemory(this->text, sizeof(this->text));
            strcpy(this->text, text);
        }

        // Set the color
        this->text_color = text_color;

        // Set the new font
        SetFont(fontPath, fontSize);

        // Load the new sprite
        SDL_Surface* loader;
        good = LoadSDLTexture(renderer, &loader, &sprite, TTF_RenderText_Blended, this->font, this->text, this->text_color);

        // Set the new width and height
        this->srcRect.w = loader->w;
        this->srcRect.h = loader->h;
        TTF_SizeText(this->font, this->text, &dstRect.w, &dstRect.h);

        // Free the sprite
        SDL_FreeSurface(loader);
    }

    void Draw(SDL_Renderer* renderer){
        if(good && visable) SDL_RenderCopy(renderer, sprite, &srcRect, &dstRect);
        else cout << "Text is not good!" << endl;
    }

    void ResizeFont(SDL_Renderer* renderer, int newSize){           
        Load(renderer, this->text, this->fontPath, newSize, text_color);
    }

    void ChangeFontFace(SDL_Renderer* renderer, const char* newPath){
        if(!strcmp(this->fontPath, newPath)){
            cout << "Cannot change old font to the same font!" << endl;
            return;
        }

        Load(renderer, this->text, newPath, this->fontSize, this->text_color);
    }

    char fontPath[50] = {0};
    TTF_Font* font;
    int fontSize;

    char text[50] = {0};

    SDL_Color text_color;
    SDL_Rect srcRect, dstRect;
    SDL_Texture* sprite;

    bool good, visable;
};

#endif

Вот функция LoadSDLTexture:

bool LoadSDLTexture(SDL_Renderer* renderer, SDL_Surface** loader, SDL_Texture** sprite, SDL_Surface* loaderFunction(TTF_Font*, const char*, SDL_Color), TTF_Font* font, const char* text, SDL_Color text_color){
// Load the surface and texture
*loader = loaderFunction(font, text, text_color);
*sprite = SDL_CreateTextureFromSurface(renderer, *loader);

// Check for loading errors
if(*loader == NULL || *sprite == NULL){
    color(12);
    cout << "Failed loading text: \"";
    color(14);
    cout << text;
    color(12);
    cout <<  "\"";
    color(7);
}

// Check for loader surface specific errors
if(!*loader){
    cout << "\n\tLoader == NULL\n\t->" << TTF_GetError() << endl;

    return false;
}

// Check for texture sprite specific errors
if(!*sprite){
    cout << "\n\tSprite == NULL\n\t" << TTF_GetError() << endl;

    return false;
}

// Print "Success" if everything is fine
color(10);
cout << "Successfully loaded text: \"";
color(11);
cout << text;
color(10);
cout << "\"\n";
color(7);

return true;
}    

Не стесняйтесь говорить, если вы видите какие-либо другие проблемы с этим классом.

...