[Bf-blender-cvs] [e3a0e57542f] master: CleanUp: DrawManager Balanced alloc/free of surface_per_mat cache

Jeroen Bakker noreply at git.blender.org
Wed Oct 7 16:18:23 CEST 2020


Commit: e3a0e57542ff153575e1d0de1dc18388406ca6a8
Author: Jeroen Bakker
Date:   Wed Oct 7 13:55:07 2020 +0200
Branches: master
https://developer.blender.org/rBe3a0e57542ff153575e1d0de1dc18388406ca6a8

CleanUp: DrawManager Balanced alloc/free of surface_per_mat cache

The alloc was done in the init, but the free was done in the discard.
As discarding can happen more often there were some unneeded if
statements to work around use after free.

This patch moves the free to the `mesh_batch_cache_clear` and removes
the if statements to check for use after free.

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

M	source/blender/draw/intern/draw_cache_impl_mesh.c

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

diff --git a/source/blender/draw/intern/draw_cache_impl_mesh.c b/source/blender/draw/intern/draw_cache_impl_mesh.c
index eb0aef40216..64879f438e3 100644
--- a/source/blender/draw/intern/draw_cache_impl_mesh.c
+++ b/source/blender/draw/intern/draw_cache_impl_mesh.c
@@ -536,20 +536,16 @@ static void mesh_batch_cache_request_surface_batches(MeshBatchCache *cache)
 {
   mesh_batch_cache_add_request(cache, MBC_SURFACE);
   DRW_batch_request(&cache->batch.surface);
-  if (cache->surface_per_mat) {
-    for (int i = 0; i < cache->mat_len; i++) {
-      DRW_batch_request(&cache->surface_per_mat[i]);
-    }
+  for (int i = 0; i < cache->mat_len; i++) {
+    DRW_batch_request(&cache->surface_per_mat[i]);
   }
 }
 
 static void mesh_batch_cache_discard_surface_batches(MeshBatchCache *cache)
 {
   GPU_BATCH_DISCARD_SAFE(cache->batch.surface);
-  if (cache->surface_per_mat) {
-    for (int i = 0; i < cache->mat_len; i++) {
-      GPU_BATCH_DISCARD_SAFE(cache->surface_per_mat[i]);
-    }
+  for (int i = 0; i < cache->mat_len; i++) {
+    GPU_BATCH_DISCARD_SAFE(cache->surface_per_mat[i]);
   }
   cache->batch_ready &= ~MBC_SURFACE;
 }
@@ -565,10 +561,6 @@ static void mesh_batch_cache_discard_shaded_tri(MeshBatchCache *cache)
   }
   mesh_batch_cache_discard_surface_batches(cache);
   mesh_cd_layers_type_clear(&cache->cd_used);
-
-  MEM_SAFE_FREE(cache->surface_per_mat);
-
-  cache->mat_len = 0;
 }
 
 static void mesh_batch_cache_discard_uvedit(MeshBatchCache *cache)
@@ -718,11 +710,11 @@ static void mesh_batch_cache_clear(Mesh *me)
   }
 
   mesh_batch_cache_discard_shaded_tri(cache);
-
   mesh_batch_cache_discard_uvedit(cache);
+  MEM_SAFE_FREE(cache->surface_per_mat);
+  cache->mat_len = 0;
 
   cache->batch_ready = 0;
-
   drw_mesh_weight_state_clear(&cache->weight_state);
 }



More information about the Bf-blender-cvs mailing list