-- Bugfix #3186: Fix memory leaks caused by multiple calls to

PyDict_SetItemString() with objects that were not properly decrefed
   afterwards.  Due to the number of places this was used, I added a
   wrapper EXPP_dict_set_item_str() to gen_utils.c to handle it.

   This started as a scriptlink bug, due to how many times scripts were
   being executed I think it just magnified how bad the memory leak in
   BPy was.  Animating the blend attached with this bug report would cause
   memory to grow by about 3MB for every 280 frames.  After the patch,
   memory did not appear to grow at all (or at least not noticably using
   Unix's ps and top utils).

   Since many of the PyDict_SetItemString() calls were in initialization
   modules I think my tests executed most of the changed code, but would
   appreciate script users really giving feedback.
This commit is contained in:
Ken Hughes
2005-11-30 08:18:06 +00:00
parent 00f4310970
commit b7a4a6c837
11 changed files with 60 additions and 47 deletions

View File

@@ -1096,11 +1096,11 @@ PyObject *BGL_Init(void)
buffer_Type.ob_type = &PyType_Type;
#define EXPP_ADDCONST(x) PyDict_SetItemString(dict, #x, PyInt_FromLong(x))
#define EXPP_ADDCONST(x) EXPP_dict_set_item_str(dict, #x, PyInt_FromLong(x))
/* So, for example:
* EXPP_ADDCONST(GL_CURRENT_BIT) becomes
* PyDict_SetItemString(dict, "GL_CURRENT_BIT", PyInt_FromLong(GL_CURRENT_BIT)) */
* EXPP_dict_set_item_str(dict, "GL_CURRENT_BIT", PyInt_FromLong(GL_CURRENT_BIT)) */
EXPP_ADDCONST(GL_CURRENT_BIT);
EXPP_ADDCONST(GL_POINT_BIT);

View File

@@ -416,13 +416,13 @@ static PyObject *Blender_Get( PyObject * self, PyObject * args )
else if( StringEqual( str, "vrmloptions" ) ) {
ret = PyDict_New( );
PyDict_SetItemString( dict, "twoside",
EXPP_dict_set_item_str( ret, "twoside",
PyInt_FromLong( U.vrmlflag & USER_VRML_TWOSIDED ) );
PyDict_SetItemString( dict, "layers",
EXPP_dict_set_item_str( ret, "layers",
PyInt_FromLong( U.vrmlflag & USER_VRML_LAYERS ) );
PyDict_SetItemString( dict, "autoscale",
EXPP_dict_set_item_str( ret, "autoscale",
PyInt_FromLong( U.vrmlflag & USER_VRML_AUTOSCALE ) );
} /* End 'quick hack' part. */
@@ -678,8 +678,8 @@ static PyObject *Blender_ShowHelp(PyObject *self, PyObject *args)
return EXPP_ReturnPyObjError(PyExc_MemoryError,
"can't create py dictionary!");
PyDict_SetItemString(rkeyd, "script", script);
PyDict_SetItemString(bpy_registryDict, "__help_browser", rkeyd);
EXPP_dict_set_item_str(rkeyd, "script", script);
EXPP_dict_set_item_str(bpy_registryDict, "__help_browser", rkeyd);
arglist = Py_BuildValue("(s)", hspath);
Blender_Run(self, arglist);
@@ -798,10 +798,10 @@ void M_Blender_Init(void)
PyModule_AddIntConstant(module, "TRUE", 1);
PyModule_AddIntConstant( module, "FALSE", 0 );
PyDict_SetItemString(dict, "bylink", EXPP_incr_ret_False());
PyDict_SetItemString(dict, "link", EXPP_incr_ret (Py_None));
PyDict_SetItemString(dict, "event", PyString_FromString(""));
PyDict_SetItemString(dict, "mode", smode);
EXPP_dict_set_item_str(dict, "bylink", EXPP_incr_ret_False());
PyDict_SetItemString(dict, "link", Py_None);
EXPP_dict_set_item_str(dict, "event", PyString_FromString(""));
EXPP_dict_set_item_str(dict, "mode", smode);
PyDict_SetItemString(dict, "Armature", Armature_Init());
PyDict_SetItemString(dict, "BezTriple", BezTriple_Init());

View File

@@ -534,12 +534,14 @@ void BPY_spacescript_do_pywin_event( SpaceScript * sc, unsigned short event,
int pass_ascii = 0;
if (ascii > 31 && ascii != 127) {
pass_ascii = 1;
PyDict_SetItemString(g_blenderdict, "event", PyInt_FromLong((long)ascii));
EXPP_dict_set_item_str(g_blenderdict, "event",
PyInt_FromLong((long)ascii));
}
exec_callback( sc, sc->script->py_event,
Py_BuildValue( "(ii)", event, val ) );
if (pass_ascii)
PyDict_SetItemString(g_blenderdict, "event", PyString_FromString(""));
EXPP_dict_set_item_str(g_blenderdict, "event",
PyString_FromString(""));
}
}
@@ -1392,11 +1394,11 @@ PyObject *Draw_Init( void )
dict = PyModule_GetDict( submodule );
#define EXPP_ADDCONST(x) \
PyDict_SetItemString(dict, #x, PyInt_FromLong(x))
EXPP_dict_set_item_str(dict, #x, PyInt_FromLong(x))
/* So, for example:
* EXPP_ADDCONST(LEFTMOUSE) becomes
* PyDict_SetItemString(dict, "LEFTMOUSE", PyInt_FromLong(LEFTMOUSE))
* EXPP_dict_set_item_str(dict, "LEFTMOUSE", PyInt_FromLong(LEFTMOUSE))
*/
EXPP_ADDCONST( LEFTMOUSE );

View File

@@ -734,7 +734,7 @@ PyObject *Effect_Init( void )
dict = PyModule_GetDict( submodule );
PyDict_SetItemString( dict, "Particle", particle );
EXPP_dict_set_item_str( dict, "Particle", particle );
return ( submodule );
}

View File

@@ -369,12 +369,12 @@ PyObject *Lattice_Init( void )
Lattice_Type.ob_type = &PyType_Type;
//Module dictionary
#define EXPP_ADDCONST(x) PyDict_SetItemString(dict, #x, PyInt_FromLong(LT_##x))
#define EXPP_ADDCONST(x) EXPP_dict_set_item_str(dict, #x, PyInt_FromLong(LT_##x))
EXPP_ADDCONST( GRID );
EXPP_ADDCONST( OUTSIDE );
#undef EXPP_ADDCONST
#define EXPP_ADDCONST(x) PyDict_SetItemString(dict, #x, PyInt_FromLong(KEY_##x))
#define EXPP_ADDCONST(x) EXPP_dict_set_item_str(dict, #x, PyInt_FromLong(KEY_##x))
EXPP_ADDCONST( LINEAR );
EXPP_ADDCONST( CARDINAL );
EXPP_ADDCONST( BSPLINE );

View File

@@ -158,8 +158,8 @@ PyObject *sys_Init( void )
if( sep ) {
Py_INCREF( sep );
PyDict_SetItemString( dict, "dirsep", sep );
PyDict_SetItemString( dict, "sep", sep );
EXPP_dict_set_item_str( dict, "dirsep", sep );
EXPP_dict_set_item_str( dict, "sep", sep );
}
return submodule;

View File

@@ -74,7 +74,7 @@ static PyObject *new_const(void)
"couldn't create constant object's dictionary"));
}
return EXPP_incr_ret((PyObject *)constant);
return (PyObject *)constant;
}
//------------------------tp_doc
//The __doc__ string for this object
@@ -230,7 +230,7 @@ PyObject *PyConstant_New(void)
//Inserts a key:value pair into the constant and then returns 0/1
int PyConstant_Insert(BPy_constant *self, char *name, PyObject *value)
{
return PyDict_SetItemString(self->dict, name, value);
return EXPP_dict_set_item_str(self->dict, name, value);
}
//This is a helper function for generating constants......
PyObject *PyConstant_NewInt(char *name, int value)

View File

@@ -904,3 +904,16 @@ PyObject *EXPP_setterWrapperTuple ( PyObject * self, PyObject * args,
return NULL;
}
/*
* Helper to keep dictionaries from causing memory leaks. When some object
* is just created to be added to the dictionary, its reference count needs
* to be decremented so it can be reclaimed.
*/
int EXPP_dict_set_item_str( PyObject *dict, char *key, PyObject *value)
{
/* add value to dictionary */
int ret = PyDict_SetItemString(dict, key, value);
Py_DECREF( value ); /* delete original */
return ret;
}

View File

@@ -142,5 +142,8 @@ PyObject *EXPP_clearScriptLinks(ScriptLink *slink, PyObject *args);
/* this queues redraws if we're not in background mode: */
void EXPP_allqueue(unsigned short event, short val);
/* helper to keep dictionaries from causing memory leaks */
int EXPP_dict_set_item_str( PyObject *dict, char *key, PyObject *value);
#endif /* EXPP_gen_utils_h */