jim-easterbrook / python-gphoto2

Python interface to libgphoto2
GNU Lesser General Public License v3.0
359 stars 59 forks source link

Memory leak in gp_camera_get_config #67

Closed jim-easterbrook closed 5 years ago

jim-easterbrook commented 5 years ago

This simple script consumes ever larger amounts of memory.

import time
import gphoto2 as gp
c = gp.Camera()
while True:
    cfg = c.get_config()
    time.sleep(0.01)

I've established that removing the gp_widget_ref call in the Python interface gets rid of the leak, but leaves the Python interface vulnerable to the C object getting deleted prematurely.

jim-easterbrook commented 5 years ago

I think I've worked out what's happening. Whenever it receives a CameraWidget object (e.g. from gp_widget_get_child) the Python interface calls gp_widget_ref on its root object, to ensure the widget tree doesn't get deleted while the Python object exists. When the Python object is deleted the interface calls gp_widget_unref on the root object. The problem is with gp_camera_get_config which returns the root object. This newly allocated object already has a ref count of 1, so calling gp_widget_ref increases it to 2. When the Python object is deleted it returns the count to 1 and the C object is not freed.

jim-easterbrook commented 5 years ago

OK, I think commit b8966a5 should cure this. @AntiSol - do you need a new release immediately? (I've got no other changes planned, so there's no great reason not to, but I don't like to do new releases too often.)

AntiSol commented 5 years ago

If you'd prefer I'm happy to give it a try without a release, that'll let me confirm that it's fixed, as long as I'm not going to have too much trouble with the "Install from GitHub" instructions from the readme - will that overwrite the version installed by pip? or should I remove the pip version first?

jim-easterbrook commented 5 years ago

Ah, I've just remembered that's a bit of a hassle. You'd need to install SWIG. I'll do a release.

AntiSol commented 5 years ago

ok. thanks :)

jim-easterbrook commented 5 years ago

Now released as v1.8.5.

AntiSol commented 5 years ago

and confirmed fixed. My hero! Thanks so much!

jim-easterbrook commented 5 years ago

Thank you for finding and reporting the bug.