diff options
author | Stephen Swaney <sswaney@centurytel.net> | 2004-10-14 21:35:16 +0400 |
---|---|---|
committer | Stephen Swaney <sswaney@centurytel.net> | 2004-10-14 21:35:16 +0400 |
commit | 7dda27fcd7266befeb9c8f191726154831438b59 (patch) | |
tree | 79e00f3b1cece2fd594ba7b3a4b87c0cfb962445 /source/blender/python | |
parent | 6fbd4e3e1f5169aed15856f7b1c412176035b236 (diff) |
followup to vector memory leak fixes:
fix for problems with NMesh vertices.
plug some more leaks in matrix module.
new vector method newVectorProxy().
In BPy-Land, we have overloaded the meaning of our Vector
type. One use is for vectors in the traditional mathmatical
sense. The other legacy use is as a proxy for Blender data.
The recent memory leak fixed have lead to the Vector type
behaving as mathematical vectors.
However, the NMesh module is still using Vector types as a
proxy to manipulate NMVert data. To support this usage, in
the vector module there is a new factory method
newVectorProxy(). This creates a Vector that references
memory outside the Vector. Vectors created by
newVectorProxy() do NOT free their referenced memory. The
newVectorProxy() is used only in bpy code and is not exposed
thru the scripting interface.
Anyone using newVectorProxy() must be aware of object
lifetime and scoping issues because the returned Vector
holds a pointer to memory it does not own. This works in
the NMVert case since we are referencing memory belonging to
the NMVert object via an NMVert method.
Diffstat (limited to 'source/blender/python')
-rw-r--r-- | source/blender/python/api2_2x/NMesh.c | 6 | ||||
-rw-r--r-- | source/blender/python/api2_2x/NMesh.h | 12 | ||||
-rw-r--r-- | source/blender/python/api2_2x/matrix.c | 14 | ||||
-rw-r--r-- | source/blender/python/api2_2x/vector.c | 33 | ||||
-rw-r--r-- | source/blender/python/api2_2x/vector.h | 1 |
5 files changed, 53 insertions, 13 deletions
diff --git a/source/blender/python/api2_2x/NMesh.c b/source/blender/python/api2_2x/NMesh.c index 541638c86e1..53bceb23cd8 100644 --- a/source/blender/python/api2_2x/NMesh.c +++ b/source/blender/python/api2_2x/NMesh.c @@ -685,12 +685,12 @@ static PyObject *NMVert_getattr( PyObject * self, char *name ) BPy_NMVert *mv = ( BPy_NMVert * ) self; if( !strcmp( name, "co" ) || !strcmp( name, "loc" ) ) - return newVectorObject( mv->co, 3 ); + return newVectorProxy( mv->co, 3 ); else if( strcmp( name, "no" ) == 0 ) - return newVectorObject( mv->no, 3 ); + return newVectorProxy( mv->no, 3 ); else if( strcmp( name, "uvco" ) == 0 ) - return newVectorObject( mv->uvco, 3 ); + return newVectorProxy( mv->uvco, 3 ); else if( strcmp( name, "index" ) == 0 ) return PyInt_FromLong( mv->index ); else if( strcmp( name, "sel" ) == 0 ) diff --git a/source/blender/python/api2_2x/NMesh.h b/source/blender/python/api2_2x/NMesh.h index a9c21ac23b2..3742ef72e07 100644 --- a/source/blender/python/api2_2x/NMesh.h +++ b/source/blender/python/api2_2x/NMesh.h @@ -75,12 +75,14 @@ void remove_vert_def_nr( Object * ob, int def_nr, int vertnum ); /* Typedefs for the new types */ typedef struct { - PyObject_HEAD unsigned char r, g, b, a; + PyObject_HEAD /* required python macro */ + unsigned char r, g, b, a; } BPy_NMCol; /* an NMesh color: [r,g,b,a] */ typedef struct { - PyObject_VAR_HEAD float co[3]; + PyObject_VAR_HEAD /* required python macro */ + float co[3]; float no[3]; float uvco[3]; int index; @@ -89,7 +91,8 @@ typedef struct { } BPy_NMVert; /* an NMesh vertex */ typedef struct { - PyObject_HEAD PyObject * v; + PyObject_HEAD /* required python macro */ + PyObject * v; PyObject *uv; PyObject *col; short mode; @@ -101,7 +104,8 @@ typedef struct { } BPy_NMFace; /* an NMesh face */ typedef struct { - PyObject_HEAD Mesh * mesh; + PyObject_HEAD /* required python macro */ + Mesh * mesh; Object *object; /* for vertex grouping info, since it's stored on the object */ PyObject *name; PyObject *materials; diff --git a/source/blender/python/api2_2x/matrix.c b/source/blender/python/api2_2x/matrix.c index efefa337d8a..d515e06c672 100644 --- a/source/blender/python/api2_2x/matrix.c +++ b/source/blender/python/api2_2x/matrix.c @@ -262,7 +262,8 @@ PyObject *Matrix_Resize4x4( MatrixObject * self ) PyObject *Matrix_TranslationPart( MatrixObject * self ) { - float *vec; + float *vec = NULL; + PyObject *retval; if( self->colSize < 3 ) { return EXPP_ReturnPyObjError( PyExc_AttributeError, @@ -282,7 +283,9 @@ PyObject *Matrix_TranslationPart( MatrixObject * self ) vec[2] = self->matrix[3][2]; } - return ( PyObject * ) newVectorObject( vec, 3 ); + retval = ( PyObject * ) newVectorObject( vec, 3 ); + PyMem_Free( vec ); + return retval; } PyObject *Matrix_RotationPart( MatrixObject * self ) @@ -556,7 +559,8 @@ static PyObject *Matrix_repr( MatrixObject * self ) //compatability static PyObject *Matrix_item( MatrixObject * self, int i ) { - float *vec; + float *vec = NULL; + PyObject *retval; int x; if( i < 0 || i >= self->rowSize ) @@ -572,7 +576,9 @@ static PyObject *Matrix_item( MatrixObject * self, int i ) vec[x] = self->matrix[i][x]; } - return ( PyObject * ) newVectorObject( vec, self->colSize ); + retval =( PyObject * ) newVectorObject( vec, self->colSize ); + PyMem_Free( vec ); + return retval; } static PyObject *Matrix_slice( MatrixObject * self, int begin, int end ) diff --git a/source/blender/python/api2_2x/vector.c b/source/blender/python/api2_2x/vector.c index 83da130b8fd..ba527352786 100644 --- a/source/blender/python/api2_2x/vector.c +++ b/source/blender/python/api2_2x/vector.c @@ -222,8 +222,8 @@ static PyObject *Vector_getattr( VectorObject * self, char *name ) self->vec[1] * self->vec[1] ) ); } else - EXPP_ReturnPyObjError( PyExc_AttributeError, - "can only return the length of a 2D ,3D or 4D vector\n" ); + return EXPP_ReturnPyObjError( PyExc_AttributeError, + "can only return the length of a 2D ,3D or 4D vector\n" ); } return Py_FindMethod( Vector_methods, ( PyObject * ) self, name ); @@ -696,3 +696,32 @@ PyObject *newVectorObject( float *vec, int size ) return ( PyObject * ) self; } + + +/* + create a Vector that is a proxy for blender data. + we do not own this data, we NEVER free it. + Note: users must deal with bad pointer issue +*/ + +PyObject *newVectorProxy( float *vec, int size) +{ + VectorObject *proxy; + int x; + + proxy = PyObject_NEW( VectorObject, &vector_Type ); + + proxy->delete_pymem = 0; /* must NOT free this alloc later */ + + if( !vec || size < 1 ) { + return EXPP_ReturnPyObjError( PyExc_AttributeError, + "cannot creat zero length vector proxy" ); + } + + proxy->vec = vec; + proxy->size = size; + proxy->flag = 0; + + return ( PyObject * ) proxy; +} + diff --git a/source/blender/python/api2_2x/vector.h b/source/blender/python/api2_2x/vector.h index cf07b5ce78e..40e5851359a 100644 --- a/source/blender/python/api2_2x/vector.h +++ b/source/blender/python/api2_2x/vector.h @@ -61,6 +61,7 @@ typedef struct { //prototypes PyObject *newVectorObject( float *vec, int size ); +PyObject *newVectorProxy( float *vec, int size ); PyObject *Vector_Zero( VectorObject * self ); PyObject *Vector_Normalize( VectorObject * self ); PyObject *Vector_Negate( VectorObject * self ); |