Fix potential memory leak in Cycles loading of packed/generated images.
authorBrecht Van Lommel <brechtvanlommel@gmail.com>
Thu, 20 Jul 2017 20:31:02 +0000 (22:31 +0200)
committerBrecht Van Lommel <brechtvanlommel@gmail.com>
Thu, 20 Jul 2017 21:46:58 +0000 (23:46 +0200)
intern/cycles/blender/blender_session.cpp

index 2b5dd5eadea636141a6276bd9ba3082fabf2257f..753b3822cab9ffcd1fafdb2b44d6c3688314fdc2 100644 (file)
@@ -1115,7 +1115,6 @@ bool BlenderSession::builtin_image_pixels(const string &builtin_name,
 
        if(image_pixels && num_pixels * channels == pixels_size) {
                memcpy(pixels, image_pixels, pixels_size * sizeof(unsigned char));
-               MEM_freeN(image_pixels);
        }
        else {
                if(channels == 1) {
@@ -1134,6 +1133,11 @@ bool BlenderSession::builtin_image_pixels(const string &builtin_name,
                        }
                }
        }
+
+       if(image_pixels) {
+               MEM_freeN(image_pixels);
+       }
+
        /* Premultiply, byte images are always straight for Blender. */
        unsigned char *cp = pixels;
        for(size_t i = 0; i < num_pixels; i++, cp += channels) {
@@ -1172,7 +1176,6 @@ bool BlenderSession::builtin_image_float_pixels(const string &builtin_name,
 
                if(image_pixels && num_pixels * channels == pixels_size) {
                        memcpy(pixels, image_pixels, pixels_size * sizeof(float));
-                       MEM_freeN(image_pixels);
                }
                else {
                        if(channels == 1) {
@@ -1192,6 +1195,10 @@ bool BlenderSession::builtin_image_float_pixels(const string &builtin_name,
                        }
                }
 
+               if(image_pixels) {
+                       MEM_freeN(image_pixels);
+               }
+
                return true;
        }
        else if(b_id.is_a(&RNA_Object)) {