Python script leaking memory

Started by
2 comments, last by thedustbustr 20 years, 8 months ago
the relevant source first as usual

import katana #my static extension module, boost code below
vector3=katana.vector3

class DummyCamera:
    position=vector3(0,1,0)
    view=vector3(0,0,0)
    up=vector3(0,1,0)

class SuperCamera:
    camera=DummyCamera()
    def __init__(self):
        self.gamecamera=katana.camera
        self.calibrate()
    def flush(self):
        """ apply the changes to the game camera """
        self.gamecamera.position.x = self.camera.position.x
        self.gamecamera.position.y = self.camera.position.y
        self.gamecamera.position.z = self.camera.position.z

        self.gamecamera.up.x = self.camera.up.x
        self.gamecamera.up.y = self.camera.up.y
        self.gamecamera.up.z = self.camera.up.z

        if not self.freelook:
            self.gamecamera.view.x = self.camera.view.x
            self.gamecamera.view.y = self.camera.view.y
            self.gamecamera.view.z = self.camera.view.z
    def update(self):
        ... #update the dummy camera
        self.flush() #flush the changes to the real camera
and the relevant boost.python wrapper code

...
using namespace boost::python;
extern Interface* systems;

Clock & GetClock() { Clock* c=systems->clock; return *c;}
Camera & GetCamera() { Camera* c=systems->camera; return *c;}

BOOST_PYTHON_MODULE(katana)
{
	class_<vector3>( "vector3", init<>() )
		.def( init<vector3>() )
		.def( init<float,float,float>() )
		.def_readwrite( "x", &vector3::x )
		.def_readwrite( "y", &vector3::y )
		.def_readwrite( "z", &vector3::z )
	;

	class_<Interface>( ... );
	class_<Camera>( "Camera", init<>() )
        ...
		.def_readwrite( "position", &Camera::position )
		.def_readwrite( "view", &Camera::view )
		.def_readwrite( "up", &Camera::up )
	;
        class_<Clock>( ... );

return_value_policy<reference_existing_object>() );
	def( "GetClock", GetClock, return_value_policy<reference_existing_object>() );
	def( "GetCamera", GetCamera, return_value_policy<reference_existing_object>() );
}
...
PyRun_SimpleString("import katana");
PyRun_SimpleString("katana.clock=katana.GetClock()");
PyRun_SimpleString("katana.camera=katana.GetCamera()");
Ok. Each time I call supercamera.flush(), memory gets leaked. It typically gets called once per frame at 125 fps, and I sit back and watch my memory usage go up 20k per second in windows task manager. I dont understand why memory is being leaked. Camera.position, .view, and .up are C++ vector3 instances. katana.camera is a c++ instance of Camera exposed to python through the GetCamera function (which is called through PyRun_SimpleString for convienence so the python user doesn''t have to call GetCamera all the time. Thanks for any light you may shed on this, I''m stupified. Dustin
Advertisement
I''m not sure what the problem is, exactly. Perhaps adding some sort of trace to your objects'' constructors and destructors might help, or running under the MSVCRT memory debugging functions if you use MSVC. Or Valgrind if in Linux?

I''m intrigued as to why you use 3 assignments for the camera changes and not just 1 assignment of the whole vector?
I use three assignments for the vectors because I didn''t expose operator= to python.

I tried cout''ing where I thought the problem was, and as soon as I added the cout statement all cout''s in the program stopped working (python print''s worked as normal). Comment out the added cout and everything works fine.

I had this problem before with a different memory problem, fixed it by accident But wow its hard to debug without cout.

Maybe if I wrote to disk it would work... Memory problems are so unpredictable.
I don't program python but from what I've read, python is a interpret language with garbage collection. The problem could be python allocate memory at each call but hasn't come to the point to collect all the garbage. You can compare the memory usage before and after your programm run. If that's the same, then there is no memory leaking.

[edited by - paladinzzz on August 19, 2003 1:01:35 AM]

This topic is closed to new replies.

Advertisement