Jump to content
  • Advertisement
Sign in to follow this  
Mybowlcut

SDL_FreeSurface

This topic is 3550 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Revisiting SDL code after a while of having nothing to do with graphics... I'm puzzled as to why this code doesn't leak:
#include <string>

#include "SDL.h"
#include "SDL_image.h"

int main(int argc, char* argv[])
{
	// Initialise SDL.
	SDL_System sys(SDL_INIT_EVERYTHING);
	// Initialise SDL VIDEO.
	SDL_Renderer renderer(".png", 640, 480, 24, "poo", "C:\\WINDOWS\\Fonts\\Arial.ttf", 14);

	std::string fn = "char_01.png";

	SDL_Surface* loaded = NULL;
	SDL_Surface* optimized = NULL; // NEVER FREED...
	Uint32 map_colour_key = 0;

	loaded = IMG_Load(fn.c_str());
	if(loaded == NULL && !fn.empty())
	{
		std::cout << "Image at: " << fn << " failed to load.\nSDL: " << SDL_GetError();
		return 0;
	}

	optimized = SDL_DisplayFormat(loaded);
	SDL_FreeSurface(loaded);
	if(optimized == NULL)
	{
		std::cout << "Image at: " << fn << " failed to optimise.\nSDL: " << SDL_GetError();
		return 0;
	}

	// Create the colour key.
	map_colour_key = SDL_MapRGB(optimized->format, 255, 255, 255);
	// Sets the colour key to the Surface.
	SDL_SetColorKey(optimized, SDL_RLEACCEL | SDL_SRCCOLORKEY, map_colour_key);

	return 0;
}

I'm using Visual Leak Detector and it says there are no leaks. :s Edit: In fact, even if I never free loaded it says there are no leaks!

Share this post


Link to post
Share on other sites
Advertisement
Then that's probably a yes. What's most likely happening is that the version of SDL you're using was built against a different version of the CRT than the one that your program is built against. Since Visual Leak Detector uses allocation hooks in the version of the CRT your program uses, then it probably just isn't seeing any of the allocations that occur inside the DLL.

Share this post


Link to post
Share on other sites
Ok. So how do I determine which CRT the SDL libraries are built with? If I understand you correctly, I'm building the debug build of my project with the /MDd option (Multi-threaded Debug DLL). Or is that the wrong idea?

Share this post


Link to post
Share on other sites
I was going to suggest using Valgrind instead, but I see that it is Linux only. Ah well, that more than makes up for not having Visual Studio :).

Anyway, if you have access to a Linux machine, you can try it out as it definitely catches things like this.

Share this post


Link to post
Share on other sites
If you want to use Visual Leak Detector, you'll need to recompile SDL from source with the same compiler you're using to build your application and build both with the Multi-thread Debug DLL version of the CRT. You'll probably want to name this version of the SDL DLL something different like SDLd.DLL.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!