[Bf-blender-cvs] [458ba77] object_nodes: Fix for static empty mesh variable, used as default value of meshes on the stack.

Lukas Tönne noreply at git.blender.org
Wed Nov 25 10:53:34 CET 2015


Commit: 458ba77ff7ecafe7488714abfa116f4eed568da1
Author: Lukas Tönne
Date:   Wed Nov 25 10:51:28 2015 +0100
Branches: object_nodes
https://developer.blender.org/rB458ba77ff7ecafe7488714abfa116f4eed568da1

Fix for static empty mesh variable, used as default value of meshes on the stack.

This creates a (minor) mem leak when statically creating the mesh. Instead it
should be constructed in a init/free pair of functions.

Because this variable is used in all different compile units, each of them has
to create their own global 'empty_mesh' variable.

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

M	source/blender/blenvm/bvm/bvm_eval.cc
M	source/blender/blenvm/bvm/bvm_eval.h
M	source/blender/blenvm/compile/bvm_nodegraph.cc
M	source/blender/blenvm/compile/bvm_nodegraph.h
M	source/blender/blenvm/intern/bvm_api.cc
M	source/blender/blenvm/util/bvm_util_typedesc.h

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

diff --git a/source/blender/blenvm/bvm/bvm_eval.cc b/source/blender/blenvm/bvm/bvm_eval.cc
index ebf424a..b6d9014 100644
--- a/source/blender/blenvm/bvm/bvm_eval.cc
+++ b/source/blender/blenvm/bvm/bvm_eval.cc
@@ -61,6 +61,8 @@ EvalContext::~EvalContext()
 
 /* ------------------------------------------------------------------------- */
 
+static mesh_ptr __empty_mesh__;
+
 static void eval_op_value_float(float *stack, float value, StackIndex offset)
 {
 	stack_store_float(stack, offset, value);
@@ -903,4 +905,14 @@ void EvalContext::eval_expression(const EvalGlobals *globals, const EvalData *da
 	}
 }
 
+void bvm_init()
+{
+	create_empty_mesh(__empty_mesh__);
+}
+
+void bvm_free()
+{
+	destroy_empty_mesh(__empty_mesh__);
+}
+
 } /* namespace bvm */
diff --git a/source/blender/blenvm/bvm/bvm_eval.h b/source/blender/blenvm/bvm/bvm_eval.h
index e505989..c582bb8 100644
--- a/source/blender/blenvm/bvm/bvm_eval.h
+++ b/source/blender/blenvm/bvm/bvm_eval.h
@@ -124,6 +124,9 @@ protected:
 	MEM_CXX_CLASS_ALLOC_FUNCS("BVM:EvalContext")
 };
 
+void bvm_init();
+void bvm_free();
+
 } /* namespace bvm */
 
 #endif /* __BVM_EVAL_H__ */
diff --git a/source/blender/blenvm/compile/bvm_nodegraph.cc b/source/blender/blenvm/compile/bvm_nodegraph.cc
index ac75db4..f68c8d4 100644
--- a/source/blender/blenvm/compile/bvm_nodegraph.cc
+++ b/source/blender/blenvm/compile/bvm_nodegraph.cc
@@ -1126,7 +1126,9 @@ OpCode get_opcode_from_node_type(const string &node)
 	return OP_NOOP;
 }
 
-void register_opcode_node_types()
+static mesh_ptr __empty_mesh__;
+
+static void register_opcode_node_types()
 {
 	NodeType *nt;
 	
@@ -1360,4 +1362,16 @@ void register_opcode_node_types()
 	nt->add_output("mesh_out", BVM_MESH, __empty_mesh__);
 }
 
+void nodes_init()
+{
+	create_empty_mesh(__empty_mesh__);
+	
+	register_opcode_node_types();
+}
+
+void nodes_free()
+{
+	destroy_empty_mesh(__empty_mesh__);
+}
+
 } /* namespace bvm */
diff --git a/source/blender/blenvm/compile/bvm_nodegraph.h b/source/blender/blenvm/compile/bvm_nodegraph.h
index 9778349..bfce899 100644
--- a/source/blender/blenvm/compile/bvm_nodegraph.h
+++ b/source/blender/blenvm/compile/bvm_nodegraph.h
@@ -372,7 +372,8 @@ public:
 };
 
 OpCode get_opcode_from_node_type(const string &node);
-void register_opcode_node_types();
+void nodes_init();
+void nodes_free();
 
 } /* namespace bvm */
 
diff --git a/source/blender/blenvm/intern/bvm_api.cc b/source/blender/blenvm/intern/bvm_api.cc
index a6e225b..06d654d 100644
--- a/source/blender/blenvm/intern/bvm_api.cc
+++ b/source/blender/blenvm/intern/bvm_api.cc
@@ -58,14 +58,30 @@ extern "C" {
 #include "bvm_util_map.h"
 #include "bvm_util_thread.h"
 
+namespace bvm {
+static mesh_ptr __empty_mesh__;
+}
+
 void BVM_init(void)
 {
-	bvm::register_opcode_node_types();
+	using namespace bvm;
+	
+	create_empty_mesh(__empty_mesh__);
+	
+	bvm_init();
+	nodes_init();
 }
 
 void BVM_free(void)
 {
+	using namespace bvm;
+	
 	BVM_texture_cache_clear();
+	
+	nodes_free();
+	bvm_free();
+	
+	destroy_empty_mesh(__empty_mesh__);
 }
 
 /* ------------------------------------------------------------------------- */
diff --git a/source/blender/blenvm/util/bvm_util_typedesc.h b/source/blender/blenvm/util/bvm_util_typedesc.h
index 77f34ff..fdb6fc0 100644
--- a/source/blender/blenvm/util/bvm_util_typedesc.h
+++ b/source/blender/blenvm/util/bvm_util_typedesc.h
@@ -298,19 +298,21 @@ struct DerivedMeshDestructor {
 
 typedef node_data_ptr<DerivedMesh, DerivedMeshDestructor> mesh_ptr;
 
-namespace detail {
-
-static mesh_ptr create_empty_mesh()
+inline void create_empty_mesh(mesh_ptr &p)
 {
 	DerivedMesh *dm = CDDM_new(0, 0, 0, 0, 0);
 	dm->needsFree = 0;
-	return mesh_ptr(dm);
+	p.set(dm);
 }
 
+inline void destroy_empty_mesh(mesh_ptr &p)
+{
+	DerivedMesh *dm = p.get();
+	dm->needsFree = 1;
+	dm->release(dm);
+	p.set(NULL);
 }
 
-static const mesh_ptr __empty_mesh__ = detail::create_empty_mesh();
-
 
 template <BVMType type>
 struct BaseTypeTraits;




More information about the Bf-blender-cvs mailing list