From 50c23cbae082f4aba50620e97ab1f08a16f9703d Mon Sep 17 00:00:00 2001 From: Freeyorp Date: Mon, 28 Sep 2009 16:46:38 +1300 Subject: Fix file handling in imagewriter Make imagewriter only open a file stream after some checks have occurred elsewhere, and be sure to close it if the surface conversion fails. --- src/resources/imagewriter.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'src/resources') diff --git a/src/resources/imagewriter.cpp b/src/resources/imagewriter.cpp index 097e0c05..86f92561 100644 --- a/src/resources/imagewriter.cpp +++ b/src/resources/imagewriter.cpp @@ -30,12 +30,6 @@ bool ImageWriter::writePNG(SDL_Surface *surface, const std::string &filename) { // TODO Maybe someone can make this look nice? - FILE *fp = fopen(filename.c_str(), "wb"); - if (!fp) - { - logger->log("could not open file %s for writing", filename.c_str()); - return false; - } png_structp png_ptr; png_infop info_ptr; @@ -68,6 +62,13 @@ bool ImageWriter::writePNG(SDL_Surface *surface, const std::string &filename) return false; } + FILE *fp = fopen(filename.c_str(), "wb"); + if (!fp) + { + logger->log("could not open file %s for writing", filename.c_str()); + return false; + } + png_init_io(png_ptr, fp); colortype = (surface->format->BitsPerPixel == 24) ? @@ -84,6 +85,7 @@ bool ImageWriter::writePNG(SDL_Surface *surface, const std::string &filename) if (!row_pointers) { logger->log("Had trouble converting surface to row pointers"); + fclose(fp); return false; } -- cgit v1.2.3-70-g09d2