Memory leak while using SDL

Can you post an image here showing the result you’re getting when rendering the text?

Do you have a SDL_RenderClear() at the beginning of your loop?

I just tried it but that wouldn’t work… I destroy the texture and surface that the text is blighted to every loop and that doesn’t work either. I think its because I draw the text every loop for some reason.

As been previously hinted - you should clear the current render target by calling the SDL_RenderClear() function at the beginning of the frame, which cleans the render target of any data from the precious frame. Not doing so can cause artefacts in/on the window/screen, like the text issue you’re experiencing.

A typical SDL mainloop looks like this:

while(Running)
{
   while(SDL_PollEvent(&Event))
   {

   }

   // Update stage, update all the objects

   // Clear the current render target
   SDL_RenderClear(pRenderer);

   // Render stage, render all the graphical objects

   // Update the window/screen
   SDL_RenderPresent(pRenderer);
}

In your case, you should basically clear the current render target, render the background and other textures, render the text (which are also textures) and the finally update the window/screen with the new data.

Hi,
Thanks again. I figured out how to fix the problem even though it doesn’t make too much sense why I had to fix it the way I did. I basically had to fix the problem by appending empty space at the end of the text. I don’t know why but when I send the text to render solid, it duplicates the text to the right over and over until the end of the sdl_rectangle width… I destroy the texture and the surface also at the end of rendering the text. I found out by playing with the output that destroying the text and surface was working and was stopping one of the memory leaks I had when I first created this topic. I also printed out the actual text I was sending to render solid and it only prints the way I would expect so I think it’s something render solid is doing when putting the text on the surface. The problem seems to come from making a surface and then texture somehow. I would guess its printing the string in the vector position over and over because its close in memory? I know C will do this if trying to print too many characters. It will print the target cell and also print adjacent cells. Just a guess.

I think there should be a SDL_SetRenderDrawColor() before the SDL_RenderClear(), but I’m almost certain that this analysis of the OP’s problem is correct, and that his isn’t.

If the RenderClear is omitted something may be ‘left over’ from the previous frame, and because of the way the renderer reuses the canvas it’s entirely possible that it will be displaced from where it was originally drawn, giving rise to the duplicated text artefact that the OP is seeing.

Re-drawing the entire frame can make the SDL_RenderClear() seemingly unnecessary, and that’s effectively what the OP’s ‘fix’ is doing by writing blanks over the region containing the left over text. But it’s not the right solution, and omitting the RenderClear is bad practice even in this case.