Fix (unreported) memleak in ImBuf mipmap code in some cases.
authorBastien Montagne <montagne29@wanadoo.fr>
Fri, 2 Dec 2016 08:44:41 +0000 (09:44 +0100)
committerBastien Montagne <montagne29@wanadoo.fr>
Fri, 2 Dec 2016 08:44:41 +0000 (09:44 +0100)
`IMB_remakemipmap` may 'shrink' the mipmap list without actually freeing
anything, so we need to check all possible levels in `imb_freemipmapImBuf`
to avoid memory leaks, not only those currently used.

source/blender/imbuf/intern/allocimbuf.c

index ef3743d9c8a85b28b316aecabdfc0e3d58dd15b8..33750478bb40aaec9d81904a55be25d47ec4113f 100644 (file)
@@ -89,11 +89,14 @@ void imb_mmap_unlock(void)
 void imb_freemipmapImBuf(ImBuf *ibuf)
 {
        int a;
-       
-       for (a = 1; a < ibuf->miptot; a++) {
-               if (ibuf->mipmap[a - 1])
-                       IMB_freeImBuf(ibuf->mipmap[a - 1]);
-               ibuf->mipmap[a - 1] = NULL;
+
+       /* Do not trust ibuf->miptot, in some cases IMB_remakemipmap can leave unfreed unused levels,
+        * leading to memory leaks... */
+       for (a = 0; a < IMB_MIPMAP_LEVELS; a++) {
+               if (ibuf->mipmap[a] != NULL) {
+                       IMB_freeImBuf(ibuf->mipmap[a]);
+                       ibuf->mipmap[a] = NULL;
+               }
        }
 
        ibuf->miptot = 0;