[Bf-blender-cvs] [48ed9fc] master: Fix memory leak in point density

Sergey Sharybin noreply at git.blender.org
Tue Feb 23 12:01:34 CET 2016


Commit: 48ed9fcb78275cb82f844ed91f9fe4d15d2570bd
Author: Sergey Sharybin
Date:   Tue Feb 23 11:58:27 2016 +0100
Branches: master
https://developer.blender.org/rB48ed9fcb78275cb82f844ed91f9fe4d15d2570bd

Fix memory leak in point density

The issue was happening when having unconnected point density which
will cache data but will not free it because there's no actual call
to the actual sampling.

Now the idea is to make sure cache is zeroed on file load and undo
and then caching via RNA will free the data if any exists. This could
leave us with a single copy of cache in the node if it's not used,
but it's quite small amount of memory and it's not leaking.

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

M	source/blender/blenloader/intern/readfile.c
M	source/blender/makesrna/intern/rna_nodetree.c
M	source/blender/nodes/shader/nodes/node_shader_tex_pointdensity.c
M	source/blender/render/extern/include/RE_render_ext.h
M	source/blender/render/intern/source/pointdensity.c

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

diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index 7b67035..3758374 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -3025,6 +3025,10 @@ static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
 					NodeShaderScript *nss = (NodeShaderScript *) node->storage;
 					nss->bytecode = newdataadr(fd, nss->bytecode);
 				}
+				else if (node->type==SH_NODE_TEX_POINTDENSITY) {
+					NodeShaderTexPointDensity *npd = (NodeShaderTexPointDensity *) node->storage;
+					memset(&npd->pd, 0, sizeof(npd->pd));
+				}
 			}
 			else if (ntree->type==NTREE_COMPOSIT) {
 				if (ELEM(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB, CMP_NODE_HUECORRECT))
diff --git a/source/blender/makesrna/intern/rna_nodetree.c b/source/blender/makesrna/intern/rna_nodetree.c
index 50ad7a4..7561a74 100644
--- a/source/blender/makesrna/intern/rna_nodetree.c
+++ b/source/blender/makesrna/intern/rna_nodetree.c
@@ -3045,8 +3045,11 @@ void rna_ShaderNodePointDensity_density_cache(bNode *self,
 		return;
 	}
 
+	/* Make sure there's no cached data. */
+	BKE_texture_pointdensity_free_data(pd);
+	RE_point_density_free(pd);
+
 	/* Create PointDensity structure from node for sampling. */
-	memset(pd, 0, sizeof(*pd));
 	BKE_texture_pointdensity_init_data(pd);
 	pd->object = (Object *)self->id;
 	pd->radius = shader_point_density->radius;
@@ -3098,6 +3101,7 @@ void rna_ShaderNodePointDensity_density_calc(bNode *self,
 
 	/* We're done, time to clean up. */
 	BKE_texture_pointdensity_free_data(pd);
+	memset(pd, 0, sizeof(*pd));
 }
 
 void rna_ShaderNodePointDensity_density_minmax(bNode *self,
diff --git a/source/blender/nodes/shader/nodes/node_shader_tex_pointdensity.c b/source/blender/nodes/shader/nodes/node_shader_tex_pointdensity.c
index 6205b0f..006bd0c 100644
--- a/source/blender/nodes/shader/nodes/node_shader_tex_pointdensity.c
+++ b/source/blender/nodes/shader/nodes/node_shader_tex_pointdensity.c
@@ -27,6 +27,10 @@
 
 #include "../node_shader_util.h"
 
+#include "BKE_texture.h"
+
+#include "RE_render_ext.h"
+
 /* **************** OUTPUT ******************** */
 
 static bNodeSocketTemplate sh_node_tex_pointdensity_in[] = {
@@ -52,6 +56,26 @@ static void node_shader_init_tex_pointdensity(bNodeTree *UNUSED(ntree),
 	node->storage = point_density;
 }
 
+static void node_shader_free_tex_pointdensity(bNode *node)
+{
+	NodeShaderTexPointDensity *point_density = node->storage;
+	PointDensity *pd = &point_density->pd;
+	RE_point_density_free(pd);
+	BKE_texture_pointdensity_free_data(pd);
+	memset(pd, 0, sizeof(*pd));
+	MEM_freeN(point_density);
+}
+
+static void node_shader_copy_tex_pointdensity(bNodeTree * UNUSED(dest_ntree),
+                                              bNode *dest_node,
+                                              bNode *src_node)
+{
+	dest_node->storage = MEM_dupallocN(src_node->storage);
+	NodeShaderTexPointDensity *point_density = dest_node->storage;
+	PointDensity *pd = &point_density->pd;
+	memset(pd, 0, sizeof(*pd));
+}
+
 /* node type definition */
 void register_node_type_sh_tex_pointdensity(void)
 {
@@ -69,8 +93,8 @@ void register_node_type_sh_tex_pointdensity(void)
 	node_type_init(&ntype, node_shader_init_tex_pointdensity);
 	node_type_storage(&ntype,
 	                  "NodeShaderTexPointDensity",
-	                  node_free_standard_storage,
-	                  node_copy_standard_storage);
+	                  node_shader_free_tex_pointdensity,
+	                  node_shader_copy_tex_pointdensity);
 
 	nodeRegisterType(&ntype);
 }
diff --git a/source/blender/render/extern/include/RE_render_ext.h b/source/blender/render/extern/include/RE_render_ext.h
index b679866..075db3a 100644
--- a/source/blender/render/extern/include/RE_render_ext.h
+++ b/source/blender/render/extern/include/RE_render_ext.h
@@ -86,5 +86,7 @@ void RE_point_density_sample(
         const bool use_render_params,
         float *values);
 
+void RE_point_density_free(struct PointDensity *pd);
+
 #endif /* __RE_RENDER_EXT_H__ */
 
diff --git a/source/blender/render/intern/source/pointdensity.c b/source/blender/render/intern/source/pointdensity.c
index 331830a..a4a0dd6 100644
--- a/source/blender/render/intern/source/pointdensity.c
+++ b/source/blender/render/intern/source/pointdensity.c
@@ -832,3 +832,8 @@ void RE_point_density_sample(
 	free_pointdensity(pd);
 	BLI_mutex_unlock(&sample_mutex);
 }
+
+void RE_point_density_free(struct PointDensity *pd)
+{
+	free_pointdensity(pd);
+}




More information about the Bf-blender-cvs mailing list