[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [10778] trunk/blender/source/blender/ python: Many long standing memory leaks fixed in the BPY api.

Campbell Barton cbarton at metavr.com
Fri May 25 18:43:25 CEST 2007


Revision: 10778
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bforge-svn&revision=10778
Author:   campbellbarton
Date:     2007-05-25 18:43:25 +0200 (Fri, 25 May 2007)

Log Message:
-----------
Many long standing memory leaks fixed in the BPY api.

Data from Armature.c and logic.c still leaks.

Mostly todo with PyList_Append adding a refcount and the bpython api not decrefing.

Also added some features needed to fix a bug in mesh_clean.py (ob.pinShape and ob.activeShape)

Modified Paths:
--------------
    trunk/blender/source/blender/python/BPY_interface.c
    trunk/blender/source/blender/python/api2_2x/Armature.c
    trunk/blender/source/blender/python/api2_2x/Bone.c
    trunk/blender/source/blender/python/api2_2x/CurNurb.c
    trunk/blender/source/blender/python/api2_2x/Curve.c
    trunk/blender/source/blender/python/api2_2x/Draw.c
    trunk/blender/source/blender/python/api2_2x/Effect.c
    trunk/blender/source/blender/python/api2_2x/Font.c
    trunk/blender/source/blender/python/api2_2x/Ipo.c
    trunk/blender/source/blender/python/api2_2x/Key.c
    trunk/blender/source/blender/python/api2_2x/Lattice.c
    trunk/blender/source/blender/python/api2_2x/Mesh.c
    trunk/blender/source/blender/python/api2_2x/NMesh.c
    trunk/blender/source/blender/python/api2_2x/Object.c
    trunk/blender/source/blender/python/api2_2x/Text.c
    trunk/blender/source/blender/python/api2_2x/Text3d.c
    trunk/blender/source/blender/python/api2_2x/Types.c
    trunk/blender/source/blender/python/api2_2x/World.c
    trunk/blender/source/blender/python/api2_2x/constant.c
    trunk/blender/source/blender/python/api2_2x/doc/Mesh.py
    trunk/blender/source/blender/python/api2_2x/doc/Object.py
    trunk/blender/source/blender/python/api2_2x/gen_utils.c
    trunk/blender/source/blender/python/api2_2x/logic.c
    trunk/blender/source/blender/python/api2_2x/sceneTimeLine.c
    trunk/blender/source/blender/python/api2_2x/windowTheme.c

Modified: trunk/blender/source/blender/python/BPY_interface.c
===================================================================
--- trunk/blender/source/blender/python/BPY_interface.c	2007-05-25 11:48:27 UTC (rev 10777)
+++ trunk/blender/source/blender/python/BPY_interface.c	2007-05-25 16:43:25 UTC (rev 10778)
@@ -877,6 +877,7 @@
 
 	if( !setup_armature_weakrefs()){
 		printf("Oops - weakref dict\n");
+		MEM_freeN( buffer );
 		return 0;
 	}
 

Modified: trunk/blender/source/blender/python/api2_2x/Armature.c
===================================================================
--- trunk/blender/source/blender/python/api2_2x/Armature.c	2007-05-25 11:48:27 UTC (rev 10777)
+++ trunk/blender/source/blender/python/api2_2x/Armature.c	2007-05-25 16:43:25 UTC (rev 10778)
@@ -118,17 +118,18 @@
 //-----------------(internal)
 static int BoneMapping_Init(PyObject *dictionary, ListBase *bones){
 	Bone *bone = NULL;
-	PyObject *py_bone = NULL;
+	PyObject *py_bone = NULL, *str;
 
 	for (bone = bones->first; bone; bone = bone->next){
 		py_bone = PyBone_FromBone(bone);
 		if (!py_bone)
 			return -1;
-
-		if(PyDict_SetItem(dictionary, 
-			PyString_FromString(bone->name), py_bone) == -1){
+		
+		str = PyString_FromString(bone->name);
+		if(PyDict_SetItem(dictionary, str, py_bone) == -1)
 			return -1;
-		}
+		
+		Py_DECREF(str);
 		Py_DECREF(py_bone);
 		if (bone->childbase.first) 
 			BoneMapping_Init(dictionary, &bone->childbase);
@@ -138,17 +139,18 @@
 //-----------------(internal)
 static int EditBoneMapping_Init(PyObject *dictionary, ListBase *editbones){
 	EditBone *editbone = NULL;
-	PyObject *py_editbone = NULL;
+	PyObject *py_editbone = NULL, *str;
 
 	for (editbone = editbones->first; editbone; editbone = editbone->next){
 		py_editbone = PyEditBone_FromEditBone(editbone);
 		if (!py_editbone)
 			return -1;
-
-		if(PyDict_SetItem(dictionary, 
-			PyString_FromString(editbone->name), py_editbone) == -1){
+		
+		str = PyString_FromString(editbone->name);
+		if(PyDict_SetItem(dictionary, str, py_editbone) == -1)
 			return -1;
-		}
+		
+		Py_DECREF(str);
 		Py_DECREF(py_editbone);
 	}
 	return 0;
@@ -215,7 +217,7 @@
 	Py_DECREF(self->bonesMap);
 	Py_DECREF(self->editbonesMap);
 	BLI_freelistN(&self->editbones); 
-	BonesDict_Type.tp_free(self);
+	PyObject_DEL( self );
 	return;
 }
 //------------------------mp_length
@@ -422,10 +424,7 @@
 //-----------------------PyBonesDict_FromPyArmature
 static PyObject *PyBonesDict_FromPyArmature(BPy_Armature *py_armature)
 {
-	BPy_BonesDict *py_BonesDict = NULL;
-
-	//create py object
-	py_BonesDict = (BPy_BonesDict *)BonesDict_Type.tp_alloc(&BonesDict_Type, 0); 
+	BPy_BonesDict *py_BonesDict = (BPy_BonesDict *)PyObject_NEW( BPy_BonesDict, &BonesDict_Type );
 	if (!py_BonesDict)
 		goto RuntimeError;
 
@@ -1067,9 +1066,9 @@
 {
 	if (self->weaklist != NULL)
         PyObject_ClearWeakRefs((PyObject *) self);
+	
 	Py_DECREF(self->Bones);
-	Armature_Type.tp_free(self);
-	return;
+	PyObject_DEL( self );
 }
 //------------------TYPE_OBECT DEFINITION--------------------------
 PyTypeObject Armature_Type = {
@@ -1126,7 +1125,7 @@
 //----------------Blender.Armature.Get()
 /* This function will return a Py_Armature when a single string is passed
 * or else it will return a {key:value} dictionary when mutliple strings are passed
-* or it will return a {key:value} dictionary of all armatures when nothing is passed*/
+* or it will return a {key:value} dictionary of all armatures when nothing is passed */
 static PyObject *M_Armature_Get(PyObject * self, PyObject * args)
 {
 	PyObject *seq = NULL, *item = NULL, *dict = NULL, *py_armature = NULL;
@@ -1291,8 +1290,12 @@
 {
 	return Armature_update((BPy_Armature*)pyarmature);
 }
-//-----------------(internal)
-//Converts a bArmature to a PyArmature
+/*-----------------(internal)
+ * Converts a bArmature to a PyArmature
+ * 
+ * WARNING!!! - MEMORY LEAK HERE, Run in a loop and loose your ram.
+ * cannot find out why but dosnt seam to be the weakref */
+
 PyObject *Armature_CreatePyObject(struct bArmature *armature)
 {
 	BPy_Armature *py_armature = NULL;
@@ -1300,15 +1303,16 @@
 	PyObject *armlist = NULL;  /* list of armature weak refs */
 	char *list_name = ARM_WEAKREF_LIST_NAME;
 
-	//create armature type
-	py_armature = (BPy_Armature*)Armature_Type.tp_alloc(&Armature_Type, 0); /*new*/
+	/*create armature type*/
+	py_armature = PyObject_NEW( BPy_Armature, &Armature_Type );
+	
 	if (!py_armature){
 		printf("Oops - can't create py armature\n");
 		goto RuntimeError;
 	}
 
+	py_armature->armature = armature;
 	py_armature->weaklist = NULL; //init the weaklist
-	py_armature->armature = armature;
 	
 	//create armature.bones
 	py_armature->Bones = (BPy_BonesDict*)PyBonesDict_FromPyArmature(py_armature);

Modified: trunk/blender/source/blender/python/api2_2x/Bone.c
===================================================================
--- trunk/blender/source/blender/python/api2_2x/Bone.c	2007-05-25 11:48:27 UTC (rev 10777)
+++ trunk/blender/source/blender/python/api2_2x/Bone.c	2007-05-25 16:43:25 UTC (rev 10778)
@@ -88,9 +88,9 @@
 {
 	if (self->editbone){
 		if (self->editbone->parent)
-			return EXPP_incr_ret(Py_True);
+			Py_RETURN_TRUE;
 		else
-			return EXPP_incr_ret(Py_False);
+			Py_RETURN_FALSE;
 	}else{
 		goto AttributeError;
 	}
@@ -105,7 +105,7 @@
 	if (self->editbone){
 		if (self->editbone->parent)
 			self->editbone->parent = NULL;
-		return EXPP_incr_ret(Py_None);
+		Py_RETURN_NONE;
 	}else{
 		goto AttributeError;
 	}
@@ -406,9 +406,10 @@
 				goto RuntimeError;
 	}
 
-	return EXPP_incr_ret(list);
+	return list;
 
 RuntimeError:
+	Py_XDECREF( list );
 	return EXPP_objError(PyExc_RuntimeError, "%s%s%s", 
 		sEditBoneError, ".options: ", "Internal failure!");
 }
@@ -515,9 +516,9 @@
 		if (self->editbone->parent)
 			return PyEditBone_FromEditBone(self->editbone->parent);
 		else
-			return EXPP_incr_ret(Py_None);
+			Py_RETURN_NONE;
 	}else{
-		return EXPP_incr_ret(Py_None); //not in the list yet can't have a parent
+		Py_RETURN_NONE; //not in the list yet can't have a parent
 	}
 }
 //------------------------EditBone.parent (set)
@@ -878,45 +879,47 @@
 		py_bone = PyBone_FromBone(bone);
 		if (py_bone == NULL)
 			return 0;
-
+		
 		if(PyList_Append(list, py_bone) == -1){
-			goto RuntimeError;
+			return 0;
 		}
+		Py_DECREF(py_bone);
 		if (bone->childbase.first) 
-			PyBone_ChildrenAsList(list, &bone->childbase);
+			if (!PyBone_ChildrenAsList(list, &bone->childbase))
+				return 0;
 	}
 	return 1;
-
-RuntimeError:
-	return EXPP_intError(PyExc_RuntimeError, "%s%s", 
-		sBoneError, "Internal error trying to wrap blender bones!");
 }
 //-------------------------Bone.hasParent()
 static PyObject *Bone_hasParent(BPy_Bone *self)
 {
 	if (self->bone->parent)
-		return EXPP_incr_ret(Py_True);
+		Py_RETURN_TRUE;
 	else
-		return EXPP_incr_ret(Py_False);
+		Py_RETURN_FALSE;
 }
 //-------------------------Bone.hasChildren()
 static PyObject *Bone_hasChildren(BPy_Bone *self)
 {
 	if (self->bone->childbase.first)
-		return EXPP_incr_ret(Py_True);
+		Py_RETURN_TRUE;
 	else
-		return EXPP_incr_ret(Py_False);
+		Py_RETURN_FALSE;
 }
 //-------------------------Bone.getAllChildren()
 static PyObject *Bone_getAllChildren(BPy_Bone *self)
 {
 	PyObject *list = PyList_New(0);
-
-	if (self->bone->childbase.first)
-		if (!PyBone_ChildrenAsList(list, &self->bone->childbase))
-			return NULL;
-	return EXPP_incr_ret(list);
+	if (!self->bone->childbase.first) {
+		/* do nothing */
+	} else if (!PyBone_ChildrenAsList(list, &self->bone->childbase)) {
+		Py_XDECREF(list);
+		EXPP_objError(PyExc_RuntimeError, "%s%s", 
+				sBoneError, "Internal error trying to wrap blender bones!");
+	}
+	return list;
 }
+
 //------------------ATTRIBUTE IMPLEMENTATIONS-----------------------------
 //------------------------Bone.name (get)
 static PyObject *Bone_getName(BPy_Bone *self, void *closure)
@@ -1044,9 +1047,10 @@
 			EXPP_GetModuleConstant("Blender.Armature", "TIP_SELECTED")) == -1)
 			goto RuntimeError;
 
-	return EXPP_incr_ret(list);
-
+	return list;
+	
 RuntimeError:
+	Py_XDECREF(list);
 	return EXPP_objError(PyExc_RuntimeError, "%s%s%s", 
 		sBoneError, "getOptions(): ", "Internal failure!");
 }
@@ -1062,7 +1066,7 @@
 	if (self->bone->parent)
 		return PyBone_FromBone(self->bone->parent);
 	else
-		return EXPP_incr_ret(Py_None);
+		Py_RETURN_NONE;
 }
 //------------------------Bone.parent (set)
 static int Bone_setParent(BPy_Bone *self, PyObject *value, void *closure)
@@ -1081,15 +1085,17 @@
 		for (bone = self->bone->childbase.first; bone; bone = bone->next){
 			py_bone = PyBone_FromBone(bone);
 			if (py_bone == NULL)
-				return 0;
-			if(PyList_Append(list, py_bone) == -1){
 				goto RuntimeError;
-			}
+			if (PyList_Append(list, py_bone) == -1)
+				goto RuntimeError;
+			Py_DECREF(py_bone);
 		}
 	}
-	return EXPP_incr_ret(list);
+	return list;
 	
 RuntimeError:
+	Py_XDECREF(list);
+	Py_XDECREF(py_bone);
 	return EXPP_objError(PyExc_RuntimeError, "%s%s", 
 		sBoneError, "Internal error trying to wrap blender bones!");
 }
@@ -1309,19 +1315,11 @@
 //Converts a struct Bone to a BPy_Bone
 PyObject *PyBone_FromBone(struct Bone *bone)
 {
-	BPy_Bone *py_Bone = NULL;
-
-	py_Bone = (BPy_Bone*)Bone_Type.tp_alloc(&Bone_Type, 0); //*new*
-	if (py_Bone == NULL)
-		goto RuntimeError;
-
+	BPy_Bone *py_Bone = ( BPy_Bone * ) PyObject_NEW( BPy_Bone, &Bone_Type );
+	
 	py_Bone->bone = bone;
 
 	return (PyObject *) py_Bone;
-
-RuntimeError:
-	return EXPP_objError(PyExc_RuntimeError, "%s%s%s", 
-		sBoneError, "PyBone_FromBone: ", "Internal Error Ocurred");
 }
 //-----------------(internal)
 //Converts a PyBone to a bBone

Modified: trunk/blender/source/blender/python/api2_2x/CurNurb.c
===================================================================
--- trunk/blender/source/blender/python/api2_2x/CurNurb.c	2007-05-25 11:48:27 UTC (rev 10777)
+++ trunk/blender/source/blender/python/api2_2x/CurNurb.c	2007-05-25 16:43:25 UTC (rev 10778)
@@ -947,8 +947,7 @@
 
 		for( i = 0; i < 4; i++ ) {
 			PyList_SetItem( pyo, i,
-					PyFloat_FromDouble( nurb->bp[index].
-							    vec[i] ) );
+					PyFloat_FromDouble( nurb->bp[index].vec[i] ) );
 		}
 
 		/* add Tilt only if curve is 3D */

Modified: trunk/blender/source/blender/python/api2_2x/Curve.c
===================================================================
--- trunk/blender/source/blender/python/api2_2x/Curve.c	2007-05-25 11:48:27 UTC (rev 10777)
+++ trunk/blender/source/blender/python/api2_2x/Curve.c	2007-05-25 16:43:25 UTC (rev 10778)
@@ -1396,7 +1396,7 @@
 							      &Curve_Type );

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list