ID free: GP & particles...
authorBastien Montagne <montagne29@wanadoo.fr>
Wed, 30 Sep 2015 19:15:20 +0000 (21:15 +0200)
committerBastien Montagne <montagne29@wanadoo.fr>
Wed, 30 Sep 2015 19:15:20 +0000 (21:15 +0200)
source/blender/blenkernel/BKE_gpencil.h
source/blender/blenkernel/BKE_particle.h
source/blender/blenkernel/intern/gpencil.c
source/blender/blenkernel/intern/library.c
source/blender/blenkernel/intern/particle.c
source/blender/editors/gpencil/gpencil_undo.c
source/blender/makesrna/intern/rna_main_api.c

index 084c5527f211076bdb2fdb5ce37a3c64a0304242..2d287172e90c4ee1a072243d78f08f1fd02b2184 100644 (file)
@@ -42,7 +42,7 @@ struct bGPDstroke;
 bool free_gpencil_strokes(struct bGPDframe *gpf);
 void free_gpencil_frames(struct bGPDlayer *gpl);
 void free_gpencil_layers(struct ListBase *list);
-void BKE_gpencil_free(struct bGPdata *gpd);
+void BKE_gpencil_free(struct bGPdata *gpd, const bool do_id_user);
 
 void gpencil_stroke_sync_selection(struct bGPDstroke *gps);
 
index 89cfb3a2aac31eff564218cdb19e9e1977f3b91a..83e589fcf77d82e881f2abaa4db42ab1c4f6c6b1 100644 (file)
@@ -297,7 +297,8 @@ void psys_check_group_weights(struct ParticleSettings *part);
 int psys_uses_gravity(struct ParticleSimulationData *sim);
 
 /* free */
-void BKE_particlesettings_free(struct ParticleSettings *part);
+void BKE_particlesettings_release_datablocks(struct ParticleSettings *part);
+void BKE_particlesettings_free(struct ParticleSettings *part, const bool do_id_user);
 void psys_free_path_cache(struct ParticleSystem *psys, struct PTCacheEdit *edit);
 void psys_free(struct Object *ob, struct ParticleSystem *psys);
 
index ee5c91923716121789da4b189d98e578a4e5e781..24c596c3ff9d7c90bde8701b9a98291841a2fbe2 100644 (file)
@@ -112,16 +112,12 @@ void free_gpencil_layers(ListBase *list)
 }
 
 /* Free all of GPencil datablock's related data, but not the block itself */
-void BKE_gpencil_free(bGPdata *gpd)
+void BKE_gpencil_free(bGPdata *gpd, const bool UNUSED(do_id_user))
 {
        /* free layers */
        free_gpencil_layers(&gpd->layers);
        
-       /* free animation data */
-       if (gpd->adt) {
-               BKE_animdata_free(&gpd->id);
-               gpd->adt = NULL;
-       }
+       BKE_animdata_free(&gpd->id);
 }
 
 /* -------- Container Creation ---------- */
index db7aca2147364ca4b5240b2f03fbde33ad3c025c..c4a8f521db5b7b69d417507579397f514a588ab9 100644 (file)
@@ -1021,14 +1021,14 @@ void BKE_libblock_free_ex(Main *bmain, void *idv, bool do_id_user)
                        BKE_brush_free((Brush *)id);
                        break;
                case ID_PA:
-                       BKE_particlesettings_free((ParticleSettings *)id);
+                       BKE_particlesettings_free((ParticleSettings *)id, do_id_user);
                        break;
                case ID_WM:
                        if (free_windowmanager_cb)
                                free_windowmanager_cb(NULL, (wmWindowManager *)id);
                        break;
                case ID_GD:
-                       BKE_gpencil_free((bGPdata *)id);
+                       BKE_gpencil_free((bGPdata *)id, do_id_user);
                        break;
                case ID_MC:
                        BKE_movieclip_free((MovieClip *)id);
index 9aacba8d02ec2322f1b776b1d914964a811080fc..c3c211ac741afde0e45770ce1df3a2ad2379e46c 100644 (file)
@@ -371,10 +371,48 @@ static void fluid_free_settings(SPHFluidSettings *fluid)
                MEM_freeN(fluid); 
 }
 
-void BKE_particlesettings_free(ParticleSettings *part)
+/**
+ * Release all datablocks (ID) used by this partsett (datablocks are never freed, they are just unreferenced).
+ *
+ * \param part The particle settings which has to release its data.
+ */
+void BKE_particlesettings_release_datablocks(ParticleSettings *part)
 {
        MTex *mtex;
        int a;
+
+       if (part == NULL)
+               return;
+
+       for (a = 0; a < MAX_MTEX; a++) {
+               mtex = part->mtex[a];
+               if (mtex && mtex->tex) {
+                       mtex->tex->id.us--;
+                       mtex->tex = NULL;
+               }
+       }
+
+       /* No ID refcount here... */
+       part->dup_group = NULL;
+       part->dup_ob = NULL;
+       part->bb_ob = NULL;
+}
+
+/**
+ * Free (or release) any data used by this particle settings (does not free the partsett itself).
+ *
+ * \param part The particle settings to free.
+ * \param do_id_user When \a true, ID datablocks used (referenced) by this partsett are 'released'
+ *                   (their user count is decreased).
+ */
+void BKE_particlesettings_free(ParticleSettings *part, const bool do_id_user)
+{
+       int a;
+
+       if (do_id_user) {
+               BKE_particlesettings_release_datablocks(part);
+       }
+
        BKE_animdata_free(&part->id);
        
        if (part->clumpcurve)
@@ -385,8 +423,7 @@ void BKE_particlesettings_free(ParticleSettings *part)
        free_partdeflect(part->pd);
        free_partdeflect(part->pd2);
 
-       if (part->effector_weights)
-               MEM_freeN(part->effector_weights);
+       MEM_SAFE_FREE(part->effector_weights);
 
        BLI_freelistN(&part->dupliweights);
 
@@ -394,9 +431,7 @@ void BKE_particlesettings_free(ParticleSettings *part)
        fluid_free_settings(part->fluid);
 
        for (a = 0; a < MAX_MTEX; a++) {
-               mtex = part->mtex[a];
-               if (mtex && mtex->tex) mtex->tex->id.us--;
-               if (mtex) MEM_freeN(mtex);
+               MEM_SAFE_FREE(part->mtex[a]);
        }
 }
 
index 34e640a4b7baf1ea81c66c6dbedd3ac4cfc3cd66..eaa0cd5563c0e76a283769b0eef2249e6de6b567 100644 (file)
@@ -140,7 +140,7 @@ void gpencil_undo_push(bGPdata *gpd)
                         */
                        undo_node->gpd->adt = NULL;
                        
-                       BKE_gpencil_free(undo_node->gpd);
+                       BKE_gpencil_free(undo_node->gpd, false);
                        MEM_freeN(undo_node->gpd);
                        
                        BLI_freelinkN(&undo_nodes, undo_node);
@@ -168,7 +168,7 @@ void gpencil_undo_finish(void)
                 */
                undo_node->gpd->adt = NULL;
                
-               BKE_gpencil_free(undo_node->gpd);
+               BKE_gpencil_free(undo_node->gpd, false);
                MEM_freeN(undo_node->gpd);
                
                undo_node = undo_node->next;
index b38f4fa67b6eab9d9ba47b704dc2de7396ece124..6e9f61a64e6027e9b785490ad34fadc618cf4652 100644 (file)
@@ -718,7 +718,6 @@ static void rna_Main_grease_pencil_remove(Main *bmain, ReportList *reports, Poin
 {
        bGPdata *gpd = gpd_ptr->data;
        if (ID_REAL_USERS(gpd) <= 0) {
-               BKE_gpencil_free(gpd);
                BKE_libblock_free(bmain, gpd);
                RNA_POINTER_INVALIDATE(gpd_ptr);
        }