Commits

illume  committed 2cd6d1c

Fixes #43. Memory leak in pygame.image.save with png files,
in the imageext.c write_png function.

Thanks Winston Ewert for the patch.
Thanks Jonathan Berry, and Marco Lettere for work on the bug.

  • Participants
  • Parent commits 719ce6d

Comments (0)

Files changed (1)

File src/imageext.c

 #ifdef PNG_H
 
 static int
-write_png (const char *file_name, png_bytep *rows, int w, int h,
-           int colortype, int bitdepth)
+write_png (const char *file_name, 
+           png_bytep *rows, 
+           int w, 
+           int h,
+           int colortype, 
+           int bitdepth)
 {
-    png_structp png_ptr;
-    png_infop info_ptr;
+    png_structp png_ptr = NULL;
+    png_infop info_ptr =  NULL;
     FILE *fp = NULL;
     char *doing = "open for writing";
 
     doing = "closing file";
     if(0 != fclose (fp))
         goto fail;
+    png_destroy_write_struct(&png_ptr, &info_ptr);
     return 0;
 
 fail:
+    /*
+     * I don't see how to handle the case where png_ptr
+     * was allocated but info_ptr was not. However, those
+     * calls should only fail if memory is out and you are
+     * probably screwed regardless then. The resulting memory
+     * leak is the least of your concerns.
+     */
+    if( png_ptr && info_ptr ) {
+        png_destroy_write_struct(&png_ptr, &info_ptr);
+    }
     SDL_SetError ("SavePNG: could not %s", doing);
     return -1;
 }