This topic is now archived and is closed to further replies.


Python script leaking memory

Recommended Posts

the relevant source first as usual
import katana #my static extension module, boost code below

class DummyCamera:

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

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

        if not self.freelook:
            self.gamecamera.view.x =
            self.gamecamera.view.y =
            self.gamecamera.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;}

	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");
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. 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

Share this post

Link to post
Share on other sites
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?

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
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]

Share this post

Link to post
Share on other sites