The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL_RenderCopy() memory leak
myteon


Joined: 20 Aug 2016
Posts: 2
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.
myteon


Joined: 20 Aug 2016
Posts: 2
I made a .bmp to illustrate the leak I am experiencing.
https://www.dropbox.com/s/9v81raldd0q3mkx/SDLRenderCopy%20Leak.bmp?dl=0
a quick note on the draw(), it contains all call chains to RenderCopy
Let me know if the link isn't working
Swinki3


Joined: 29 Aug 2016
Posts: 1
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")); <---------------- [b]first implict malloc() - for surface[/b]
   format = surfaceRender->format;
   SDL_SetColorKey(surfaceRender, true, SDL_MapRGB(format, 255, 0, 255));
   textures.push_back((SDL_CreateTextureFromSurface(render, surfaceRender))); <-[b]conversion into texture - second malloc, for texture, but you'll it later[/b]
   
   
   surfaceRender = IMG_Load(("Images\\arrows.xcf")); <-----------------[b] third malloc() - another surface[/b]
   format = surfaceRender->format;
   SDL_SetColorKey(surfaceRender, true, SDL_MapRGB(format, 255, 0, 255));
   textures.push_back((SDL_CreateTextureFromSurface(render, surfaceRender))); <- [b]conversion - fourth mallco, again you'l need this data later[/b]
   
   SDL_FreeSurface(surfaceRender); <-----------------[b]-releasing mem but only the second surface[/b]
}


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()); [b] <---------------------- this could be possible source of leak if this creates texture and doesn't release[/b]
   // 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.