[arrayfire] 49/248: use RAII to avoid freeimage bitmap resource leaks

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Tue Nov 17 15:53:54 UTC 2015


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch dfsg-clean
in repository arrayfire.

commit e737b52b7031be88b193f14c342047e2648d57b5
Author: Vardan Akopian <vakopian+github at gmail.com>
Date:   Fri Sep 11 01:16:37 2015 -0700

    use RAII to avoid freeimage bitmap resource leaks
---
 src/api/c/imageio.cpp | 36 ++++++++++++++++++++++++++++++------
 1 file changed, 30 insertions(+), 6 deletions(-)

diff --git a/src/api/c/imageio.cpp b/src/api/c/imageio.cpp
index 98af2e0..3c19e49 100644
--- a/src/api/c/imageio.cpp
+++ b/src/api/c/imageio.cpp
@@ -58,6 +58,24 @@ static void FI_Init()
     static FI_Manager manager = FI_Manager();
 }
 
+class FI_BitmapResource
+{
+public:
+    explicit FI_BitmapResource(FIBITMAP * p) :
+        pBitmap(p)
+    {
+    }
+
+    ~FI_BitmapResource()
+    {
+        FreeImage_Unload(pBitmap);
+    }
+private:
+    FIBITMAP * pBitmap;
+};
+
+
+
 // Helpers
 void FreeImageErrorHandler(FREE_IMAGE_FORMAT oFif, const char* zMessage);
 
@@ -202,6 +220,9 @@ af_err af_load_image(af_array *out, const char* filename, const bool isColor)
             AF_ERROR("FreeImage Error: Error reading image or file does not exist", AF_ERR_RUNTIME);
         }
 
+        // make sure pBitmap is unleaded automatically, no matter how we exit this function
+        FI_BitmapResource bitmapUnloader(pBitmap);
+
         // check image color type
         uint color_type = FreeImage_GetColorType(pBitmap);
         const uint fi_bpp = FreeImage_GetBPP(pBitmap);
@@ -277,7 +298,6 @@ af_err af_load_image(af_array *out, const char* filename, const bool isColor)
             }
         }
 
-        FreeImage_Unload(pBitmap);
         std::swap(*out,rImage);
     } CATCHALL;
 
@@ -324,6 +344,9 @@ af_err af_save_image(const char* filename, const af_array in_)
             AF_ERROR("FreeImage Error: Error creating image or file", AF_ERR_RUNTIME);
         }
 
+        // make sure pResultBitmap is unleaded automatically, no matter how we exit this function
+        FI_BitmapResource resultBitmapUnloader(pResultBitmap);
+
         // FI assumes [0-255]
         // If array is in 0-1 range, multiply by 255
         af_array in;
@@ -431,8 +454,6 @@ af_err af_save_image(const char* filename, const af_array in_)
             AF_ERROR("FreeImage Error: Failed to save image", AF_ERR_RUNTIME);
         }
 
-        FreeImage_Unload(pResultBitmap);
-
         if(free_in) AF_CHECK(af_release_array(in ));
         if(rr != 0) AF_CHECK(af_release_array(rr ));
         if(gg != 0) AF_CHECK(af_release_array(gg ));
@@ -486,6 +507,9 @@ af_err af_load_image_memory(af_array *out, const void* ptr)
             AF_ERROR("FreeImage Error: Error reading image or file does not exist", AF_ERR_RUNTIME);
         }
 
+        // make sure pBitmap is unleaded automatically, no matter how we exit this function
+        FI_BitmapResource bitmapUnloader(pBitmap);
+
         // check image color type
         uint color_type = FreeImage_GetColorType(pBitmap);
         const uint fi_bpp = FreeImage_GetBPP(pBitmap);
@@ -542,7 +566,6 @@ af_err af_load_image_memory(af_array *out, const void* ptr)
                 AF_CHECK((readImage<float, 3, 3>)(&rImage, pSrcLine, nSrcPitch, fi_w, fi_h));
         }
 
-        FreeImage_Unload(pBitmap);
         std::swap(*out,rImage);
     } CATCHALL;
 
@@ -584,6 +607,9 @@ af_err af_save_image_memory(void **ptr, const af_array in_, const af_image_forma
             AF_ERROR("FreeImage Error: Error creating image or file", AF_ERR_RUNTIME);
         }
 
+        // make sure pResultBitmap is unleaded automatically, no matter how we exit this function
+        FI_BitmapResource resultBitmapUnloader(pResultBitmap);
+
         // FI assumes [0-255]
         // If array is in 0-1 range, multiply by 255
         af_array in;
@@ -695,8 +721,6 @@ af_err af_save_image_memory(void **ptr, const af_array in_, const af_image_forma
 
         *ptr = stream;
 
-        FreeImage_Unload(pResultBitmap);
-
         if(free_in) AF_CHECK(af_release_array(in ));
         if(rr != 0) AF_CHECK(af_release_array(rr ));
         if(gg != 0) AF_CHECK(af_release_array(gg ));

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/arrayfire.git



More information about the debian-science-commits mailing list