I'm currently trying to reduce the memory leak my program is causing and the process has me kind of dumbfounded.
I understand that everything you create using "new" you have to delete eventually so that there is no memory leak. The same goes for temporary pointers (right)?
Anyway I've located a part of my code that is leaking, to no surprise it's where i update one of the UI elements in my game. I'm going to simplify the code here because the question isn't about only that code but rather how i handle a situation like this (i have loads of them in my code at the moment).
So here goes:
void update(int timer)
{
//this is my update function which updates this certain element
//i have a reference to my loader class already loaded in, it's called imageLoader in this example. imageLoader stores all of the loaded
//images and can return a pointer to a certain image on function calls.
SDL_Surface* tempSurface = imageLoader.loadImg("example_image.png");
//tempSurface now stores a pointer to the image that is returned from loadImg();
SDL_BlirSurface(tempSurface, NULL, screen, NULL);
//now I've used the img and the local tempSurface is useless
//Now, if tempSurface only was a int* i could have just ignored it since it's just a pointer (right)?
//but as i understands it a SDL_Surface creates other pointers within itself that must be destroyed, and if i call
//SDL_FreeSurface(tempSurface); i will destroy the surface that it's pointing to, creating problems for other classes.
//memory is leaking here, and my guess is on the "tempSurface" which i am unable to destroy without killing it's reference!!!
}
How do i make sure i free all the memory possible without corrupting/deleting the reference?
I want to make sure that i have this same problem with TTF_Font, which is part of a "sub-library" to SDL.
I really appreciate any help i can get!