SDL_RenderCopy() memory leak

Hello,

I see a spike in memory usage, about 1.5M, every time SDL_RenderCopy is called. I was wondering, what I am doing wrong?

The code for loading textures into memory:

Code:
void Textures::loadSprites(SDL_Renderer* render)
{
surfaceRender = IMG_Load((“Images\suits.xcf”));
format = surfaceRender->format;
SDL_SetColorKey(surfaceRender, true, SDL_MapRGB(format, 255, 0, 255));
textures.push_back((SDL_CreateTextureFromSurface(render, surfaceRender)));
surfaceRender = IMG_Load((“Images\arrows.xcf”));
format = surfaceRender->format;
SDL_SetColorKey(surfaceRender, true, SDL_MapRGB(format, 255, 0, 255));
textures.push_back((SDL_CreateTextureFromSurface(render, surfaceRender)));
SDL_FreeSurface(surfaceRender);
}

How each object keeps track of which texture and which part of the texture it needs to render:

Code:
class RenderData
{
SDL_Texture* myTexture;
SDL_Rect* myRenderRect;

};

How an object passes its data to the tool object for rendering

Code:
void Card::render()
{
SDL_Rect rec = { 40, 40, 40, 40 };
tool->render(&myPos, myRenderData->getMyRenderRect(), myRenderData->getMyTexture(), false);
tool->textTool->renderText(std::to_string(cardIdent.value), myPos.xCoord, myPos.yCoord, tool->camera.get(),
&rec, tool->getgRender());
// testing code
//tool->renderCoordMarker(myPos.xCoord, myPos.yCoord);
//tool->renderRenderBoxOutline(myPos, myRenderData->getMyRenderRect()->w, myRenderData->getMyRenderRect()->h);
}

And lastly the actual call to SDL_RenderCopy and where the leak is occurring:

Code:
void SDLToolBox::render(Coordinate* myCoord, SDL_Rect* myRect, SDL_Texture* myTexture, bool flip)
{
renderQ = { (myCoord->xCoord - camera->getViewPortX()) / camera->getZoom(),
(myCoord->yCoord - camera->getViewPortY()) / camera->getZoom(),
(myRect->w) / camera->getZoom(), (myRect->h) / camera->getZoom() };
if (flip)
SDL_RenderCopyEx(gRender, myTexture, myRect, &renderQ, 0, NULL, SDL_FLIP_HORIZONTAL);
else
SDL_RenderCopy(gRender, myTexture, myRect, &renderQ);
}

If any clarifications on my part are needed I’ll be back in about 8 hours.
Thank you for your time and input.

I made a .bmp to illustrate the leak I am experiencing.

a quick note on the draw(), it contains all call chains to RenderCopy
Let me know if the link isn’t working

Looking briefly at your code I can see two possible sources of memory leak.

First one is obvious:

Code:
void Textures::loadSprites(SDL_Renderer* render)
{
surfaceRender = IMG_Load((“Images\suits.xcf”)); <---------------- first implict malloc() - for surface
format = surfaceRender->format;
SDL_SetColorKey(surfaceRender, true, SDL_MapRGB(format, 255, 0, 255));
textures.push_back((SDL_CreateTextureFromSurface(render, surfaceRender))); <-conversion into texture - second malloc, for texture, but you’ll it later

surfaceRender = IMG_Load((“Images\arrows.xcf”)); <----------------- third malloc() - another surface
format = surfaceRender->format;
SDL_SetColorKey(surfaceRender, true, SDL_MapRGB(format, 255, 0, 255));
textures.push_back((SDL_CreateTextureFromSurface(render, surfaceRender))); <- conversion - fourth mallco, again you’l need this data later

SDL_FreeSurface(surfaceRender); <------------------releasing mem but only the second surface
}

This code is lack of release of the first loaded surface - it was not freed, so it results in mem leak.
But this doesn’t explain huge memory leak you’ve reported.

As you haven’t uploaded whole code,I suspect that another leak could be hidden in string rendering routine:

Code:
void Card::render()
{
SDL_Rect rec = { 40, 40, 40, 40 };
tool->render(&myPos, myRenderData->getMyRenderRect(), myRenderData->getMyTexture(), false);
tool->textTool->renderText(std::to_string(cardIdent.value), myPos.xCoord, myPos.yCoord, tool->camera.get(),
&rec, tool->getgRender()); <---------------------- this could be possible source of leak if this creates texture and doesn’t release
// testing code
//tool->renderCoordMarker(myPos.xCoord, myPos.yCoord);
//tool->renderRenderBoxOutline(myPos, myRenderData->getMyRenderRect()->w, myRenderData->getMyRenderRect()->h);
}

If you use lib similar to SDL_ttf for text rendering, it creates string to texture, allocating another chunk of memory. If this is the case, this texture needs releasing after rendering

Last but not least - you do not use of any error checking - it could be another source of errors.

I hope I could help.