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.

Comments (0)

Files changed (1)

 #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;
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.