Re: SDL_RenderCopy() memory leak
"Swinki3" <[email protected]>
| Newsgroups | gmane.comp.lib.sdl |
|---|---|
| Message-ID | <[email protected]> |
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.
_______________________________________________
SDL mailing list
[email protected]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org