I guess if you want to see my use case. I was just loading image from a texture from something I was working with SDL. I needed to improve performance somewhat so I cached the surface data to construct larger textures so I make less draw calls. The SDL docs say that the surface needs to be free'd before the pixel data and thus the destruction can potentially go out of order. Anyways thanks for the feedback.
shared_ptr<SDL_Texture>
createTextureFromFile(
string fname,
SDL_Renderer *renderer,
i32 *width,
i32 *height,
shared_ptr<SDL_Surface> *resultSurface
) {
Assert(renderer);
Assert(width);
Assert(height);
i32 components;
stbi_uc *pixelData = stbi_load(fname.c_str(), width, height, &components, 0);
Assert(pixelData);
Assert(*width);
Assert(*height);
shared_ptr<SDL_Surface> tempSurface{SDL_CreateRGBSurfaceFrom(
pixelData,
*width, *height,
components * 8,
*width * components,
255,
255 << 8,
255 << 16,
255 << 24),
SDL_FreeSurface};
Assert(tempSurface);
if (resultSurface)
{
*resultSurface = tempSurface;
}
shared_ptr<SDL_Texture> texture(
SDL_CreateTextureFromSurface(renderer, tempSurface.get()), SDL_DestroyTexture);
Assert(texture);
return texture;
}```