SDL memory leak

Multi tool use
Multi tool use


SDL memory leak



So I tryed to make something on SDL, but on first programm I have memory lear (idk leak or not) so there is some code:


#include <stdio.h>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#define SCREENSIZEX 180
#define SCREENSIZEY 300
SDL_Window* mainwind = NULL;
SDL_Renderer* rend = NULL;
TTF_Font* Usefont = NULL;

int main(int argc, char* argv)
{
SDL_Init(SDL_INIT_EVERYTHING);
Uint32 windowflags;
windowflags = SDL_WINDOW_SHOWN;
mainwind = SDL_CreateWindow("FooBar",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
SCREENSIZEX,
SCREENSIZEY,
windowflags);
rend = SDL_CreateRenderer(mainwind, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(rend, 255, 255, 255, 255);
int imgFlags = IMG_INIT_PNG;
IMG_Init(imgFlags);
TTF_Init();
Usefont = TTF_OpenFont("DOTMBold.TTF",90);

SDL_Surface* TextSurf = NULL;
SDL_Texture* TextTexture = NULL;

SDL_Color UsingColor;
UsingColor.r=0;
UsingColor.g=255;
UsingColor.b=255;
UsingColor.a=100;

bool exit = false;
char Text[500];
int counter = 0;
SDL_Event evneet;
while(!exit)
{
SDL_PollEvent(&evneet);
SDL_RenderClear(rend);
counter++;
TextSurf = TTF_RenderUTF8_Blended(Usefont, Text, UsingColor);
TextTexture = SDL_CreateTextureFromSurface(rend, TextSurf);
SDL_FreeSurface(TextSurf);
TextSurf = NULL;
SDL_RenderCopy(rend, TextTexture, NULL, NULL);
TextTexture = NULL;
SDL_DestroyTexture(TextTexture);
SDL_RenderPresent(rend);
}
SDL_FreeSurface(TextSurf);
TextSurf = NULL;
SDL_DestroyTexture(TextTexture);
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(mainwind);
SDL_Quit();
return 0;
}



Problem:
some screenshots



Idk how to fix this and tryed to do a lot of freeing and memory manipulations.
This programm do only one task. Just counting frames (in code only 0 displayed)
Its 3rd my try to make rendering and always i got the same.
Please help!





Your chart doesn't necessarily show a memory leak; what exactly does it measure?
– Ctx
Jul 1 at 21:26





Why do you set TextTexture to NULL before call to SDL_DestroyTexture ?
– Paul Ankman
Jul 1 at 22:15


TextTexture


NULL


SDL_DestroyTexture





Why do you create textures every frame?.. that will destroy performance.
– Fredrik
Jul 2 at 5:45





Is that your actual code, or errors are introduced by bad copy-paste (in that case, quesion will make no sense)? If it is, then it is rather strange why in addition to resetting texture pointer before destroying it (which seems like the source of your resource leak problems), you also have uninitialised Text array and unclear means of breaking your main loop - not shown in question, but SDL_PollEvent return value should not be ignored.
– keltar
Jul 2 at 9:05


Text


SDL_PollEvent




1 Answer
1



This looks suspicious:


while(!exit)
{
...
TextTexture = SDL_CreateTextureFromSurface(rend, TextSurf);
...
TextTexture = NULL; // A
SDL_DestroyTexture(TextTexture); // B
...
}



SDL_DestroyTexture() doesn't get a valid handle here, but a NULL-Pointer is passed. You have to swap the lines A and B, so the Texture can be freed properly.


SDL_DestroyTexture()


NULL






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

IduSYUlZjZ,BbL0UA uKWeTA7Rik0P1l3kjvf0,IAx4H,q,mS5Hfkql,eeOk 6te8sz
Q,83bpmFHrz2YjeWxVEE,6iiwdHM,j4ti8dWG,iI19ZVHhN,kudnR7,iurWJl 5T,O7u0ti9r5POOH

Popular posts from this blog

Rothschild family

Cinema of Italy