[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [28225] trunk/blender/source/blender/ python/generic: [#22045] Memory leak in Mathutils.Matrix

Campbell Barton ideasman42 at gmail.com
Fri Apr 16 10:17:13 CEST 2010


Revision: 28225
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=28225
Author:   campbellbarton
Date:     2010-04-16 10:17:13 +0200 (Fri, 16 Apr 2010)

Log Message:
-----------
[#22045] Memory leak in Mathutils.Matrix
own fault when adding mathutils callbacks, generic destructor didnt free the matrix accessor array, made the array apart of the matrix struct since its not worth malloc'ing to save at most 16bytes.

Modified Paths:
--------------
    trunk/blender/source/blender/python/generic/mathutils.h
    trunk/blender/source/blender/python/generic/mathutils_color.h
    trunk/blender/source/blender/python/generic/mathutils_euler.h
    trunk/blender/source/blender/python/generic/mathutils_matrix.c
    trunk/blender/source/blender/python/generic/mathutils_matrix.h
    trunk/blender/source/blender/python/generic/mathutils_quat.h
    trunk/blender/source/blender/python/generic/mathutils_vector.h

Modified: trunk/blender/source/blender/python/generic/mathutils.h
===================================================================
--- trunk/blender/source/blender/python/generic/mathutils.h	2010-04-16 08:14:20 UTC (rev 28224)
+++ trunk/blender/source/blender/python/generic/mathutils.h	2010-04-16 08:17:13 UTC (rev 28225)
@@ -33,26 +33,29 @@
 
 #include <Python.h>
 
-#include "mathutils_vector.h"
-#include "mathutils_matrix.h"
-#include "mathutils_quat.h"
-#include "mathutils_euler.h"
-#include "mathutils_color.h"
-
 /* Can cast different mathutils types to this, use for generic funcs */
 
 extern char BaseMathObject_Wrapped_doc[];
 extern char BaseMathObject_Owner_doc[];
 
+#define BASE_MATH_MEMBERS(_data) \
+	PyObject_VAR_HEAD \
+	float *_data;				/* array of data (alias), wrapped status depends on wrapped status */ \
+	PyObject *cb_user;			/* if this vector references another object, otherwise NULL, *Note* this owns its reference */ \
+	unsigned char cb_type;		/* which user funcs do we adhere to, RNA, GameObject, etc */ \
+	unsigned char cb_subtype;	/* subtype: location, rotation... to avoid defining many new functions for every attribute of the same type */ \
+	unsigned char wrapped;		/* wrapped data type? */ \
+
 typedef struct {
-	PyObject_VAR_HEAD
-	float *data;					/*array of data (alias), wrapped status depends on wrapped status */
-	PyObject *cb_user;					/* if this vector references another object, otherwise NULL, *Note* this owns its reference */
-	unsigned char cb_type;	/* which user funcs do we adhere to, RNA, GameObject, etc */
-	unsigned char cb_subtype;		/* subtype: location, rotation... to avoid defining many new functions for every attribute of the same type */
-	unsigned char wrapped;		/* wrapped data type? */
+	BASE_MATH_MEMBERS(data)
 } BaseMathObject;
 
+#include "mathutils_vector.h"
+#include "mathutils_matrix.h"
+#include "mathutils_quat.h"
+#include "mathutils_euler.h"
+#include "mathutils_color.h"
+
 PyObject *BaseMathObject_getOwner( BaseMathObject * self, void * );
 PyObject *BaseMathObject_getWrapped( BaseMathObject *self, void * );
 void BaseMathObject_dealloc(BaseMathObject * self);

Modified: trunk/blender/source/blender/python/generic/mathutils_color.h
===================================================================
--- trunk/blender/source/blender/python/generic/mathutils_color.h	2010-04-16 08:14:20 UTC (rev 28224)
+++ trunk/blender/source/blender/python/generic/mathutils_color.h	2010-04-16 08:17:13 UTC (rev 28225)
@@ -37,14 +37,7 @@
 #define ColorObject_Check(_v) PyObject_TypeCheck((_v), &color_Type)
 
 typedef struct {
-	PyObject_VAR_HEAD
-	float *col;					/*1D array of data */
-	PyObject *cb_user;			/* if this vector references another object, otherwise NULL, *Note* this owns its reference */
-	unsigned char cb_type;		/* which user funcs do we adhere to, RNA, GameObject, etc */
-	unsigned char cb_subtype;	/* subtype: location, rotation... to avoid defining many new functions for every attribute of the same type */
-	unsigned char wrapped;		/* wrapped data type? */
-	/* end BaseMathObject */
-
+	BASE_MATH_MEMBERS(col);
 } ColorObject;
 
 /*struct data contains a pointer to the actual data that the

Modified: trunk/blender/source/blender/python/generic/mathutils_euler.h
===================================================================
--- trunk/blender/source/blender/python/generic/mathutils_euler.h	2010-04-16 08:14:20 UTC (rev 28224)
+++ trunk/blender/source/blender/python/generic/mathutils_euler.h	2010-04-16 08:17:13 UTC (rev 28225)
@@ -37,14 +37,7 @@
 #define EulerObject_Check(_v) PyObject_TypeCheck((_v), &euler_Type)
 
 typedef struct {
-	PyObject_VAR_HEAD 
-	float *eul;					/*1D array of data */
-	PyObject *cb_user;			/* if this vector references another object, otherwise NULL, *Note* this owns its reference */
-	unsigned char cb_type;		/* which user funcs do we adhere to, RNA, GameObject, etc */
-	unsigned char cb_subtype;	/* subtype: location, rotation... to avoid defining many new functions for every attribute of the same type */
-	unsigned char wrapped;		/* wrapped data type? */
-	/* end BaseMathObject */
-
+	BASE_MATH_MEMBERS(eul);
 	unsigned char order;		/* rotation order */
 
 } EulerObject;

Modified: trunk/blender/source/blender/python/generic/mathutils_matrix.c
===================================================================
--- trunk/blender/source/blender/python/generic/mathutils_matrix.c	2010-04-16 08:14:20 UTC (rev 28224)
+++ trunk/blender/source/blender/python/generic/mathutils_matrix.c	2010-04-16 08:17:13 UTC (rev 28225)
@@ -118,7 +118,7 @@
 	float scalar;
 
 	argSize = PyTuple_GET_SIZE(args);
-	if(argSize > 4){	//bad arg nums
+	if(argSize > MATRIX_MAX_DIM) {	//bad arg nums
 		PyErr_SetString(PyExc_AttributeError, "mathutils.Matrix(): expects 0-4 numeric sequences of the same size\n");
 		return NULL;
 	} else if (argSize == 0) { //return empty 4D matrix
@@ -321,11 +321,6 @@
 		PyErr_SetString(PyExc_MemoryError, "matrix.resize4x4(): problem allocating pointer space");
 		return NULL;
 	}
-	self->matrix = PyMem_Realloc(self->matrix, (sizeof(float *) * 4));
-	if(self->matrix == NULL) {
-		PyErr_SetString(PyExc_MemoryError, "matrix.resize4x4(): problem allocating pointer space");
-		return NULL;
-	}
 	/*set row pointers*/
 	for(x = 0; x < 4; x++) {
 		self->matrix[x] = self->contigPtr + (x * 4);
@@ -1425,12 +1420,6 @@
 
 	if(type == Py_WRAP){
 		self->contigPtr = mat;
-		/*create pointer array*/
-		self->matrix = PyMem_Malloc(rowSize * sizeof(float *));
-		if(self->matrix == NULL) { /*allocation failure*/
-			PyErr_SetString( PyExc_MemoryError, "matrix(): problem allocating pointer space");
-			return NULL;
-		}
 		/*pointer array points to contigous memory*/
 		for(x = 0; x < rowSize; x++) {
 			self->matrix[x] = self->contigPtr + (x * colSize);
@@ -1442,13 +1431,6 @@
 			PyErr_SetString( PyExc_MemoryError, "matrix(): problem allocating pointer space\n");
 			return NULL;
 		}
-		/*create pointer array*/
-		self->matrix = PyMem_Malloc(rowSize * sizeof(float *));
-		if(self->matrix == NULL) { /*allocation failure*/
-			PyMem_Free(self->contigPtr);
-			PyErr_SetString( PyExc_MemoryError, "matrix(): problem allocating pointer space");
-			return NULL;
-		}
 		/*pointer array points to contigous memory*/
 		for(x = 0; x < rowSize; x++) {
 			self->matrix[x] = self->contigPtr + (x * colSize);

Modified: trunk/blender/source/blender/python/generic/mathutils_matrix.h
===================================================================
--- trunk/blender/source/blender/python/generic/mathutils_matrix.h	2010-04-16 08:14:20 UTC (rev 28224)
+++ trunk/blender/source/blender/python/generic/mathutils_matrix.h	2010-04-16 08:17:13 UTC (rev 28225)
@@ -34,21 +34,14 @@
 
 extern PyTypeObject matrix_Type;
 #define MatrixObject_Check(_v) PyObject_TypeCheck((_v), &matrix_Type)
+#define MATRIX_MAX_DIM 4
 
-typedef float **ptRow;
-typedef struct _Matrix { /* keep aligned with BaseMathObject in mathutils.h */
-	PyObject_VAR_HEAD
-	float *contigPtr;	/*1D array of data (alias)*/
-	PyObject *cb_user;	/* if this vector references another object, otherwise NULL, *Note* this owns its reference */
-	unsigned char cb_type;	/* which user funcs do we adhere to, RNA, GameObject, etc */
-	unsigned char cb_subtype;	/* subtype: location, rotation... to avoid defining many new functions for every attribute of the same type */
-	unsigned char wrapped;	/*is wrapped data?*/
-	/* end BaseMathObject */
+typedef struct {
+	BASE_MATH_MEMBERS(contigPtr);
 
 	unsigned char rowSize;
 	unsigned int colSize;
-	ptRow			matrix;		/*ptr to the contigPtr (accessor)*/
-
+	float *matrix[MATRIX_MAX_DIM];		/* ptr to the contigPtr (accessor) */
 } MatrixObject;
 
 /*struct data contains a pointer to the actual data that the

Modified: trunk/blender/source/blender/python/generic/mathutils_quat.h
===================================================================
--- trunk/blender/source/blender/python/generic/mathutils_quat.h	2010-04-16 08:14:20 UTC (rev 28224)
+++ trunk/blender/source/blender/python/generic/mathutils_quat.h	2010-04-16 08:17:13 UTC (rev 28225)
@@ -36,15 +36,8 @@
 extern PyTypeObject quaternion_Type;
 #define QuaternionObject_Check(_v) PyObject_TypeCheck((_v), &quaternion_Type)
 
-typedef struct { /* keep aligned with BaseMathObject in mathutils.h */
-	PyObject_VAR_HEAD 
-	float *quat;				/* 1D array of data (alias) */
-	PyObject *cb_user;			/* if this vector references another object, otherwise NULL, *Note* this owns its reference */
-	unsigned char cb_type;		/* which user funcs do we adhere to, RNA, GameObject, etc */
-	unsigned char cb_subtype;	/* subtype: location, rotation... to avoid defining many new functions for every attribute of the same type */
-	unsigned char wrapped;		/* wrapped data type? */
-	/* end BaseMathObject */
-
+typedef struct {
+	BASE_MATH_MEMBERS(quat);
 } QuaternionObject;
 
 /*struct data contains a pointer to the actual data that the

Modified: trunk/blender/source/blender/python/generic/mathutils_vector.h
===================================================================
--- trunk/blender/source/blender/python/generic/mathutils_vector.h	2010-04-16 08:14:20 UTC (rev 28224)
+++ trunk/blender/source/blender/python/generic/mathutils_vector.h	2010-04-16 08:17:13 UTC (rev 28225)
@@ -36,14 +36,8 @@
 extern PyTypeObject vector_Type;
 #define VectorObject_Check(_v) PyObject_TypeCheck((_v), &vector_Type)
 
-typedef struct { /* keep aligned with BaseMathObject in mathutils.h */
-	PyObject_VAR_HEAD 
-	float *vec;					/*1D array of data (alias), wrapped status depends on wrapped status */
-	PyObject *cb_user;					/* if this vector references another object, otherwise NULL, *Note* this owns its reference */
-	unsigned char cb_type;	/* which user funcs do we adhere to, RNA, GameObject, etc */
-	unsigned char cb_subtype;		/* subtype: location, rotation... to avoid defining many new functions for every attribute of the same type */
-	unsigned char wrapped;		/* wrapped data type? */
-	/* end BaseMathObject */
+typedef struct {
+	BASE_MATH_MEMBERS(vec);
 
 	unsigned char size;			/* vec size 2,3 or 4 */
 } VectorObject;





More information about the Bf-blender-cvs mailing list