We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 720

Summary: possible memory leak,use sdl and sdl_image lib
Product: SDL_image Reporter: peter <zhuxiaoqiang>
Component: miscAssignee: Sam Lantinga <slouken>
Status: RESOLVED INVALID QA Contact: Sam Lantinga <slouken>
Severity: critical    
Priority: P2    
Version: 1.2.6   
Hardware: x86   
OS: Linux   
Bug Depends on:    
Bug Blocks: 12    
Attachments: a png with alpha channel

Description peter 2009-03-26 00:04:24 UTC
Created attachment 308 [details]
a png with alpha channel

Possible Memory Leak rendering PNG images,the parts code of a sample is belows:
//============================================================================
/* Initialize the SDL library */
	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
		return(255);
	}

	flags = SDL_SWSURFACE;
	SDL_Surface* Surf_Temp = NULL;
		/* Open the image file */
	for(j=0;j<100;j++)
	{
		image[j]  = IMG_Load(argv[1]);	

        }

	
		/* Use the deepest native mode, except that we emulate 32bpp
		   for viewing non-indexed images on 8bpp screens */
	
	screen = SDL_SetVideoMode(720, 576, 16, flags);
	if ( screen == NULL ) {
		fprintf(stderr,"Couldn't set video mode: %s\n",
			SDL_GetError());
		return -1;
	}
#if 0		
		/* Set the palette, if one exists */
		if ( image[20]->format->palette ) {
			SDL_SetColors(screen, image[20]->format->palette->colors,
			              0, image[20]->format->palette->ncolors);
		}

		/* Draw a background pattern if the surface has transparency */
		if(image[20]->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY))
	    		draw_background(screen);
#endif
		/* Display the image */
		SDL_BlitSurface(image[20], NULL, screen, NULL);
		SDL_UpdateRect(screen,0,0,0,0);
		sleep(5);
		fprintf(stderr,"start free resource...................\n");

		for(j=0;j<100;j++)
		{
			printf("before free:image[%d].refcount is %d\n",j,image[j]->refcount); 
			SDL_FreeSurface(image[j]);
			printf("after free:image[%d].refcount is %d\n",j,image[j]->refcount);
			
		}

SDL_Quit();
...............
Comment 1 peter 2009-03-26 00:05:48 UTC
I don't know if it's a memory leak or something else. How can I fix it.
thanks for your help!
Comment 2 Sam Lantinga 2009-09-26 01:39:52 UTC
It's not legal to check the refcount on a surface after it was freed. :)