SDL3 - Possible Memory leak with SDL_CreateTextureFromSurface

So I wanted to put this here, as I’ve been beating my head against it. It seems like a bug, but I wanted to make sure I’m not missing something (as is often the case). I’ve worked with SDL2 for some time (though only as a side hobby) and I wanted to give SDL3 a whirl.

When I use SDL_CreateTextureFromSurface(), the resulting texture when destroyed does not seem to return all of the memory back to the system. When racing, I’m getting a memory leak that expands by about 2-3MB per second. And even given time, it never stabilizes, continuing to grow.

I’ve run the exact same program creating a dummy surface directly (SDL_CreateTexture()) and the memory use is dead stable.

Code below:

#include “SDL3/SDL.h”
#include “SDL3/SDL_ttf.h”

int main(int argc, char* args)
{
SDL_Window* window;
SDL_Renderer* render;

SDL_Init(SDL_INIT_VIDEO|SDL_INIT_EVENTS);

window = SDL_CreateWindow("Window", 1600,1200,0);

render = SDL_CreateRenderer(window, NULL, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(render, 0x00, 0x00, 0x00, 0x00);
SDL_SetRenderDrawBlendMode(render,SDL_BLENDMODE_NONE);

//=========FONTS=========

TTF_Init();
TTF_Font * font;
font = TTF_OpenFont("assets/MyriadPro-Bold.otf",50);
SDL_Color color = {255,255,255};


bool running = true;

SDL_RenderClear(render);
while(running)
{
    // Event Loop
    SDL_Event e;
    if (SDL_PollEvent(&e) > 0)
    {
        switch(e.type)
        {
            case(SDL_EVENT_QUIT):
                running = false;
                break;
        }
    }

    // Create Surface with Text
    SDL_Surface * surface = TTF_RenderUTF8_Blended(font,"Test Text",color);

    // Test 1: Create Texture from Surface
    SDL_Texture * texture = SDL_CreateTextureFromSurface(render,surface);
    // Test 2: Create Texture Manually
    // SDL_Texture * texture = SDL_CreateTexture(render,SDL_PIXELFORMAT_RGBA8888,SDL_TEXTUREACCESS_STATIC,10000,10000);
    
    // Destroy Surface
    SDL_DestroySurface(surface);
    
    // Clear Render, Draw Texture, Show Texture
    SDL_RenderClear(render);
    SDL_RenderTexture(render,texture,NULL,NULL);
    SDL_RenderPresent(render);

    // Destroy Texture
    SDL_DestroyTexture(texture);
}
return 0;

}

For the record, I tried the exact same code (with the renamed functions) in a fresh project with SDL2.30, and had no memory leak…

Any thoughts and I would be most appreciative.

Someone else reported the same issue on github a few days ago.

Maybe it’s because SDL_CreateTextureFromSurface creates some properties without freeing them which is different compared to how it’s done in SDL_CreateTexture.

Ah, didn’t see that someone else found the same thing. At least I know I’m probably not crazy - at least in this instance :slight_smile:

I wonder if there is any sort of work-around in the short term - try to release those properties? Interesting…

I’ll keep an eye on the issue and see if it gets addressed.

Thanks!