[Bf-blender-cvs] [12e5b9420ee] soc-2021-simulation-display: Fix: Memory leak in non primitive collision shape drawing functions

soumya pochiraju noreply at git.blender.org
Wed Sep 1 15:25:00 CEST 2021


Commit: 12e5b9420ee7c5304aba7e94904ea785b47857b3
Author: soumya pochiraju
Date:   Wed Sep 1 18:52:35 2021 +0530
Branches: soc-2021-simulation-display
https://developer.blender.org/rB12e5b9420ee7c5304aba7e94904ea785b47857b3

Fix: Memory leak in non primitive collision shape drawing functions

The ojects containing the non primitive collision shape meshes are stored in
a GSet along with other draw cache shapes and the meshes are freed using these
object pointers

===================================================================

M	source/blender/draw/engines/overlay/overlay_extra.c
M	source/blender/draw/intern/draw_cache.c
M	source/blender/draw/intern/draw_cache.h

===================================================================

diff --git a/source/blender/draw/engines/overlay/overlay_extra.c b/source/blender/draw/engines/overlay/overlay_extra.c
index b19c4c253fd..9a48ac3a39f 100644
--- a/source/blender/draw/engines/overlay/overlay_extra.c
+++ b/source/blender/draw/engines/overlay/overlay_extra.c
@@ -382,6 +382,7 @@ static void OVERLAY_non_primitive_collision_shape(OVERLAY_ExtraCallBuffers *cb,
                   BKE_rigidbody_store_trimesh_draw_data(ob);
                   break;
               }
+           DRW_cache_non_primitive_col_shape_store_ob(ob);
           }
           GPUBatch *geom = DRW_cache_non_primitive_col_shape_get(ob);
           if(geom){
diff --git a/source/blender/draw/intern/draw_cache.c b/source/blender/draw/intern/draw_cache.c
index 63d7ba47895..761935a187b 100644
--- a/source/blender/draw/intern/draw_cache.c
+++ b/source/blender/draw/intern/draw_cache.c
@@ -39,6 +39,8 @@
 
 #include "BKE_object.h"
 #include "BKE_paint.h"
+#include "BKE_mesh.h"
+#include "BKE_lib_id.h"
 
 #include "GPU_batch.h"
 #include "GPU_batch_utils.h"
@@ -155,16 +157,20 @@ static struct DRWShapeCache {
   GPUBatch *drw_particle_axis;
   GPUBatch *drw_gpencil_dummy_quad;
   GPUBatch *drw_sphere_lod[DRW_LOD_MAX];
+  struct GSet *non_primitive_col_shapes;
 } SHC = {NULL};
 
 void DRW_shape_cache_free(void)
 {
-  uint i = sizeof(SHC) / sizeof(GPUBatch *);
+  uint i = (sizeof(SHC) - sizeof(GSet*)) / sizeof(GPUBatch *);
   GPUBatch **batch = (GPUBatch **)&SHC;
   while (i--) {
     GPU_BATCH_DISCARD_SAFE(*batch);
     batch++;
   }
+  BLI_gset_free(SHC.non_primitive_col_shapes, (void (*)(void *key))DRW_cache_non_primitive_col_shape_free);
+  SHC.non_primitive_col_shapes = NULL;
+
 }
 
 /* -------------------------------------------------------------------- */
@@ -3607,9 +3613,19 @@ GPUBatch *DRW_cache_cursor_get(bool crosshair_lines)
   return *drw_cursor;
 }
 
-GPUBatch *DRW_cache_non_primitive_col_shape_get(Object *ob) {
+/* Store the object so it can be freed later. */
+void DRW_cache_non_primitive_col_shape_store_ob(Object *ob)
+{
+    if(!SHC.non_primitive_col_shapes) {
+        SHC.non_primitive_col_shapes = BLI_gset_ptr_new(__func__);
+    }
+    BLI_gset_add(SHC.non_primitive_col_shapes, ob);
+}
+
+GPUBatch *DRW_cache_non_primitive_col_shape_get(Object *ob)
+{
 
-    GPUBatch *geom;
+    GPUBatch *geom = NULL;
     if(ob->rigidbody_object->col_shape_draw_data != NULL){
         const DRWContextState *draw_ctx = DRW_context_state_get();
         DRW_mesh_batch_cache_validate(ob->rigidbody_object->col_shape_draw_data);
@@ -3624,6 +3640,15 @@ GPUBatch *DRW_cache_non_primitive_col_shape_get(Object *ob) {
     return geom;
 }
 
+void DRW_cache_non_primitive_col_shape_free(Object *ob)
+{
+    if (ob->rigidbody_object->col_shape_draw_data != NULL) {
+        BKE_mesh_free(ob->rigidbody_object->col_shape_draw_data);
+        BKE_id_free(NULL, ob->rigidbody_object->col_shape_draw_data);
+        ob->rigidbody_object->col_shape_draw_data = NULL;
+    }
+}
+
 /** \} */
 
 /* -------------------------------------------------------------------- */
diff --git a/source/blender/draw/intern/draw_cache.h b/source/blender/draw/intern/draw_cache.h
index ac5173bf5e0..706c00cf328 100644
--- a/source/blender/draw/intern/draw_cache.h
+++ b/source/blender/draw/intern/draw_cache.h
@@ -268,3 +268,5 @@ void DRW_cache_gpencil_sbuffer_clear(struct Object *ob);
 
 /* Non primitive collision shapes */
 struct GPUBatch *DRW_cache_non_primitive_col_shape_get(Object *ob);
+void DRW_cache_non_primitive_col_shape_free(Object *ob);
+void DRW_cache_non_primitive_col_shape_store_ob(Object *ob);



More information about the Bf-blender-cvs mailing list