Revision: 36757
          
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=36757
Author:   elubie
Date:     2011-05-18 19:42:30 +0000 (Wed, 18 May 2011)
Log Message:
-----------
fix [#27158] Appending crash with preview.
* fixed incorrect reading of PreviewImage struct from .blend 
* fixed memory leak, PreviewImage data wasn't correctly free'd

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/BKE_icons.h
    trunk/blender/source/blender/blenkernel/intern/icons.c
    trunk/blender/source/blender/blenloader/BLO_readfile.h
    trunk/blender/source/blender/blenloader/intern/readblenentry.c
    trunk/blender/source/blender/blenloader/intern/readfile.c
    trunk/blender/source/blender/editors/space_file/filelist.c

Modified: trunk/blender/source/blender/blenkernel/BKE_icons.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_icons.h 2011-05-18 19:34:17 UTC 
(rev 36756)
+++ trunk/blender/source/blender/blenkernel/BKE_icons.h 2011-05-18 19:42:30 UTC 
(rev 36757)
@@ -52,6 +52,7 @@
 typedef struct Icon Icon;
 
 struct PreviewImage;
+struct ID;
 
 void BKE_icons_init(int first_dyn_id);
 
@@ -74,11 +75,14 @@
 /* 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_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 */

Modified: trunk/blender/source/blender/blenkernel/intern/icons.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/icons.c      2011-05-18 
19:34:17 UTC (rev 36756)
+++ trunk/blender/source/blender/blenkernel/intern/icons.c      2011-05-18 
19:42:30 UTC (rev 36757)
@@ -132,18 +132,26 @@
        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;
        }
 }

Modified: trunk/blender/source/blender/blenloader/BLO_readfile.h
===================================================================
--- trunk/blender/source/blender/blenloader/BLO_readfile.h      2011-05-18 
19:34:17 UTC (rev 36756)
+++ trunk/blender/source/blender/blenloader/BLO_readfile.h      2011-05-18 
19:42:30 UTC (rev 36757)
@@ -49,6 +49,8 @@
 struct SpaceImaSel;
 struct UserDef;
 struct bContext;
+struct BHead;
+struct FileData;
 
 typedef struct BlendHandle     BlendHandle;
 
@@ -225,6 +227,8 @@
 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);

Modified: trunk/blender/source/blender/blenloader/intern/readblenentry.c
===================================================================
--- trunk/blender/source/blender/blenloader/intern/readblenentry.c      
2011-05-18 19:34:17 UTC (rev 36756)
+++ trunk/blender/source/blender/blenloader/intern/readblenentry.c      
2011-05-18 19:42:30 UTC (rev 36757)
@@ -148,15 +148,14 @@
        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,32 +173,30 @@
                } 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;
+                                       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);
                                        }
-                                       
-                                       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;
-                                       }
                                }
                        }
                } else if (bhead->code==ENDB) {

Modified: trunk/blender/source/blender/blenloader/intern/readfile.c
===================================================================
--- trunk/blender/source/blender/blenloader/intern/readfile.c   2011-05-18 
19:34:17 UTC (rev 36756)
+++ trunk/blender/source/blender/blenloader/intern/readfile.c   2011-05-18 
19:42:30 UTC (rev 36757)
@@ -13030,6 +13030,11 @@
        *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)

Modified: trunk/blender/source/blender/editors/space_file/filelist.c
===================================================================
--- trunk/blender/source/blender/editors/space_file/filelist.c  2011-05-18 
19:34:17 UTC (rev 36756)
+++ trunk/blender/source/blender/editors/space_file/filelist.c  2011-05-18 
19:42:30 UTC (rev 36757)
@@ -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 @@
 
        return BKE_idcode_from_name(buf);
 }
-
+ 
 void filelist_from_library(struct FileList* filelist)
 {
        LinkNode *l, *names, *previews;
@@ -1086,7 +1087,7 @@
        }
 
        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);
 

_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to