Memory leak

alex116321 at my-deja.com alex116321 at my-deja.com
Fri Jan 5 10:40:05 EST 2001


Actually you are correct about the memory usage but thats my fault, I
gave a poor example.  Firstly, I'm using Python 1.5.1 embedded in my
C++ code.  What I do is call a Python script using the following code:

==================================================================
	PyObject* module_str = NULL;
	PyObject* module_wincom = NULL;
	PyObject* pArgs = NULL;
	PyObject* pResult = NULL;

	//*
	//* Create a new sub-interpretor(thread) to process the
callback function
	//*
	PyEval_AcquireLock( );

	PyThreadState* pTState = Py_NewInterpreter( );

	// Note that this application has multiple threads.
	// It maintains a separate interp (sub-interpreter) for each
thread.
//	PyThreadState_Swap( pTState );

	// load the Python XML DOM COM module
	module_str = PyImport_ImportModule( "string" );
	module_wincom = PyImport_ImportModule( "win32com.client" );

	// Call Python callback functions and pass the argument in
	if ( m_fDOMObj == true )
		pArgs = Py_BuildValue( "(Osi)", _pObj, _pszMsg,
_fErrorMsg );
	else
		pArgs = Py_BuildValue( "(si)", _pszMsg, _fErrorMsg );

	pResult = PyEval_CallObject( m_pPyCallbackFunc, pArgs );

	Py_XDECREF(module_str);
	Py_XDECREF(module_wincom);
	Py_XDECREF(pArgs);
	Py_XDECREF(pResult);

	// Get sub-interpreter for current thread and destroy it.
	Py_EndInterpreter( pTState );

	PyEval_ReleaseLock( );
===================================================================

This piece of code is run each time a particular condition is met.  The
Python script (actually a Python procedure) is evaluated and memory
usage goes up.  This continues until the resources have been exhaused.
The Python script contains many COM calls to the XML Parser as the
previous example shows (of course not as simple).  I thought that when
Py_EndInterpreter is called, memory should be released.  I call
Py_Initialize when the program is initialized and Py_Finalize when it
terminates.  This is done so a couple of Python variables keep their
values each time the script is called.

Alex

In article <3A5275B8.3020208 at ActiveState.com>,
  Mark Hammond <MarkH at ActiveState.com> wrote:
> alex116321 at my-deja.com wrote:
>
> > I am experiencing memory leakage when using Python and COM.
> > Specifically, I am using the Micosoft XML Parser to load and
traverse
> > XML DOM stuctures.  Everytime I access an XML node, more memory is
> > consumed.  After a while, this can eat away most of the system
> > resources if the Python session is not terminated.  Here is a simple
> > example (assume the interpreter is used as a server and does not
exit):
>
> You don't say what version of anything.
>
> With the latest versions, (and using "xrange" instead of range) I see
no
> leaks at all - perfectly flat memory usage.
>
> Mark.
>
>


Sent via Deja.com
http://www.deja.com/



More information about the Python-list mailing list