fix [#27158] Appending crash with preview.
authorAndrea Weikert <elubie@gmx.net>
Wed, 18 May 2011 19:42:30 +0000 (19:42 +0000)
committerAndrea Weikert <elubie@gmx.net>
Wed, 18 May 2011 19:42:30 +0000 (19:42 +0000)
* fixed incorrect reading of PreviewImage struct from .blend
* fixed memory leak, PreviewImage data wasn't correctly free'd

source/blender/blenkernel/BKE_icons.h
source/blender/blenkernel/intern/icons.c
source/blender/blenloader/BLO_readfile.h
source/blender/blenloader/intern/readblenentry.c
source/blender/blenloader/intern/readfile.c
source/blender/editors/space_file/filelist.c

index 5404402..3c12a5d 100644 (file)
@@ -52,6 +52,7 @@ struct Icon
 typedef struct Icon Icon;
 
 struct PreviewImage;
+struct ID;
 
 void BKE_icons_init(int first_dyn_id);
 
@@ -74,11 +75,14 @@ void BKE_icon_changed(int icon_id);
 /* free all icons */
 void BKE_icons_free(void);
 
+/* free the preview image for use in list */
+void BKE_previewimg_freefunc(void *link);
+
 /* free the preview image */
 void BKE_previewimg_free(struct PreviewImage **prv);
 
 /* free the preview image belonging to the id */
-void BKE_previewimg_free_id(ID *id);
+void BKE_previewimg_free_id(struct ID *id);
 
 /* create a new preview image */
 struct PreviewImage* BKE_previewimg_create(void) ;
@@ -87,6 +91,6 @@ struct PreviewImage* BKE_previewimg_create(void) ;
 struct PreviewImage* BKE_previewimg_copy(struct PreviewImage *prv);
 
 /* retrieve existing or create new preview image */
-PreviewImage* BKE_previewimg_get(ID *id);
+struct PreviewImage* BKE_previewimg_get(struct ID *id);
 
 #endif /*  BKE_ICONS_H */
index 18eda83..9effd25 100644 (file)
@@ -132,18 +132,26 @@ struct PreviewImage* BKE_previewimg_create(void)
        return prv_img;
 }
 
-void BKE_previewimg_free(PreviewImage **prv)
+void BKE_previewimg_freefunc(void *link)
 {
-       if(prv && (*prv)) {
+       PreviewImage *prv = (PreviewImage *)link;
+       if (prv) {
                int i;
-               
+
                for (i=0; i<NUM_ICON_SIZES;++i) {
-                       if ((*prv)->rect[i]) {
-                               MEM_freeN((*prv)->rect[i]);
-                               (*prv)->rect[i] = NULL;
+                       if (prv->rect[i]) {
+                               MEM_freeN(prv->rect[i]);
+                               prv->rect[i] = NULL;
                        }
                }
-               MEM_freeN((*prv));
+               MEM_freeN(prv);
+       }
+}
+
+void BKE_previewimg_free(PreviewImage **prv)
+{
+       if(prv && (*prv)) {
+               BKE_previewimg_freefunc(*prv);
                *prv = NULL;
        }
 }
index b7a1e0d..6267b81 100644 (file)
@@ -49,6 +49,8 @@ struct SpaceFile;
 struct SpaceImaSel;
 struct UserDef;
 struct bContext;
+struct BHead;
+struct FileData;
 
 typedef struct BlendHandle     BlendHandle;
 
@@ -225,6 +227,8 @@ struct Main* BLO_library_append_begin(const struct bContext *C, BlendHandle** bh
 int BLO_library_append_named_part(const struct bContext *C, struct Main *mainl, BlendHandle** bh, const char *idname, int idcode, short flag);
 void BLO_library_append_end(const struct bContext *C, struct Main *mainl, BlendHandle** bh, int idcode, short flag);
 
+void *BLO_library_read_struct(struct FileData *fd, struct BHead *bh, const char *blockname);
+
 /* deprecated */
 #if 1
 void BLO_script_library_append(BlendHandle **bh, char *dir, char *name, int idcode, short flag, struct Main *mainvar, struct Scene *scene, struct ReportList *reports);
index 028835e..4ce5685 100644 (file)
@@ -148,15 +148,14 @@ LinkNode *BLO_blendhandle_get_previews(BlendHandle *bh, int ofblocktype, int *to
        LinkNode *previews= NULL;
        BHead *bhead;
        int looking=0;
-       int npreviews = 0;
        PreviewImage* prv = NULL;
        PreviewImage* new_prv = NULL;
        int tot= 0;
        
        for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
                if (bhead->code==ofblocktype) {
-                       ID *id= (ID*) (bhead+1);
-                       switch(GS(id->name))
+                       char *idname= bhead_id_name(fd, bhead);
+                       switch(GS(idname))
                        {
                                case ID_MA: /* fall through */
                                case ID_TE: /* fall through */
@@ -174,31 +173,29 @@ LinkNode *BLO_blendhandle_get_previews(BlendHandle *bh, int ofblocktype, int *to
                } else if (bhead->code==DATA) {
                        if (looking) {
                                if (bhead->SDNAnr == DNA_struct_find_nr(fd->filesdna, "PreviewImage") ) {
-                                       prv = (PreviewImage*) (bhead+1);
-                                       npreviews = 0;                          
-                                       memcpy(new_prv, prv, sizeof(PreviewImage));
-                                       if (prv->rect[0]) {
-                                               unsigned int *rect = NULL;
-                                               // int rectlen = 0;
-                                               new_prv->rect[0] = MEM_callocN(new_prv->w[0]*new_prv->h[0]*sizeof(unsigned int), "prvrect");
-                                               bhead= blo_nextbhead(fd, bhead);
-                                               rect = (unsigned int*)(bhead+1);
-                                               // rectlen = new_prv->w[0]*new_prv->h[0]*sizeof(unsigned int);
-                                               memcpy(new_prv->rect[0], rect, bhead->len);                                     
-                                       } else {
-                                               new_prv->rect[0] = NULL;
-                                       }
-                                       
-                                       if (prv->rect[1]) {
-                                               unsigned int *rect = NULL;
-                                               // int rectlen = 0;
-                                               new_prv->rect[1] = MEM_callocN(new_prv->w[1]*new_prv->h[1]*sizeof(unsigned int), "prvrect");
-                                               bhead= blo_nextbhead(fd, bhead);
-                                               rect = (unsigned int*)(bhead+1);
-                                               // rectlen = new_prv->w[1]*new_prv->h[1]*sizeof(unsigned int);
-                                               memcpy(new_prv->rect[1], rect, bhead->len);                                                     
-                                       } else {
-                                               new_prv->rect[1] = NULL;
+                                       prv = BLO_library_read_struct(fd, bhead, "PreviewImage");       
+                                       if (prv) {
+                                               memcpy(new_prv, prv, sizeof(PreviewImage));
+                                               if (prv->rect[0]) {
+                                                       unsigned int *rect = NULL;
+                                                       new_prv->rect[0] = MEM_callocN(new_prv->w[0]*new_prv->h[0]*sizeof(unsigned int), "prvrect");
+                                                       bhead= blo_nextbhead(fd, bhead);
+                                                       rect = (unsigned int*)(bhead+1);
+                                                       memcpy(new_prv->rect[0], rect, bhead->len);                                     
+                                               } else {
+                                                       new_prv->rect[0] = NULL;
+                                               }
+                                               
+                                               if (prv->rect[1]) {
+                                                       unsigned int *rect = NULL;
+                                                       new_prv->rect[1] = MEM_callocN(new_prv->w[1]*new_prv->h[1]*sizeof(unsigned int), "prvrect");
+                                                       bhead= blo_nextbhead(fd, bhead);
+                                                       rect = (unsigned int*)(bhead+1);
+                                                       memcpy(new_prv->rect[1], rect, bhead->len);                                                     
+                                               } else {
+                                                       new_prv->rect[1] = NULL;
+                                               }
+                                               MEM_freeN(prv);
                                        }
                                }
                        }
index 08b63dd..c25f9d9 100644 (file)
@@ -13030,6 +13030,11 @@ void BLO_library_append_end(const bContext *C, struct Main *mainl, BlendHandle**
        *bh= (BlendHandle*)fd;
 }
 
+void *BLO_library_read_struct(FileData *fd, BHead *bh, const char *blockname)
+{
+       return read_struct(fd, bh, blockname);
+}
+
 /* ************* READ LIBRARY ************** */
 
 static int mainvar_count_libread_blocks(Main *mainvar)
index 4daaea4..9b65589 100644 (file)
@@ -59,6 +59,7 @@
 #include "BKE_context.h"
 #include "BKE_global.h"
 #include "BKE_library.h"
+#include "BKE_icons.h"
 #include "BKE_main.h"
 #include "BKE_report.h"
 #include "BLO_readfile.h"
@@ -999,7 +1000,7 @@ static int groupname_to_code(char *group)
 
        return BKE_idcode_from_name(buf);
 }
-
 void filelist_from_library(struct FileList* filelist)
 {
        LinkNode *l, *names, *previews;
@@ -1086,7 +1087,7 @@ void filelist_from_library(struct FileList* filelist)
        }
 
        BLI_linklist_free(names, free);
-       if (previews) BLI_linklist_free(previews, (void(*)(void*)) MEM_freeN);
+       if (previews) BLI_linklist_free(previews, BKE_previewimg_freefunc);
 
        filelist_sort(filelist, FILE_SORT_ALPHA);