Coder Social home page Coder Social logo

Comments (16)

jim-easterbrook avatar jim-easterbrook commented on June 24, 2024

Sorry but I don't have time to look into this now, or for the rest of the weekend. Can you wait a day or two?
PS The list-cameras.py example shows another way to detect a connected camera.

from python-gphoto2.

flyte avatar flyte commented on June 24, 2024

Yep, sure. I was hoping I was doing something obviously wrong though :)

from python-gphoto2.

jim-easterbrook avatar jim-easterbrook commented on June 24, 2024

Memory leaks in SWIG generated Python interfaces can be a bit hard to track down, especially with a library that has its own reference counting stuff. Is this happening with Python2 or Python3 or both?

from python-gphoto2.

flyte avatar flyte commented on June 24, 2024

It's happening in Python 2 and 3. Here's a far simpler program to reproduce it (run without any cameras plugged in):

import gphoto2 as gp

context = gp.gp_context_new()
while True:
    camera = gp.check_result(gp.gp_camera_new())
    gp.gp_camera_init(camera, context)

It seems to be caused by gp.gp_camera_init(camera, context) as it doesn't leak without that line.

from python-gphoto2.

jim-easterbrook avatar jim-easterbrook commented on June 24, 2024

When I run your delightfully short script I get a lot of messages swig/python detected a memory leak of type 'Camera *', no destructor found. if I'm using the PYTHON_GPHOTO2_NO_BUILTIN=1 build. With the normal build I don't get the error messages but still have the leak. I probably won't have time to sort this out today, but I'm confident of tracking it down tomorrow.

from python-gphoto2.

jim-easterbrook avatar jim-easterbrook commented on June 24, 2024

Ignore that - the messages were because I wasn't correctly installing the PYTHON_GPHOTO2_NO_BUILTIN=1 version. When installed correctly the -builtin option makes no difference to the memory leak and generates no error messages.

from python-gphoto2.

jim-easterbrook avatar jim-easterbrook commented on June 24, 2024

I've tried adding some gp_camera_unref calls to the Python interface to gp_camera_init but it makes no difference. I suspect the leak may be in libgphoto2 itself. I found this discussion which suggests there was a problem in version 2.4. http://gphoto-software.10949.n7.nabble.com/Memory-Leaks-with-libgphoto2-4-5-td8318.html

from python-gphoto2.

flyte avatar flyte commented on June 24, 2024

Ah, well that's interesting. Sorry if I've wasted your time! I suppose the way forward here is to document a workaround in the examples. I'll see if I can come up with something, but as far as I'm aware, whatever you do with a new camera object, it calls gp_camera_init first. I'll try a few things out this morning.

from python-gphoto2.

jim-easterbrook avatar jim-easterbrook commented on June 24, 2024

No time wasted - I've learnt a bit more about libgphoto2. It appears the leak is caused if you call camera.init without having first called camera.set_port_info to select the camera.
You could try something like this:

context = gp.Context()
camera_list = []
for name, addr in context.camera_autodetect():
    camera_list.append((name, addr))
if camera_list:
    camera = gp.Camera()
    port_info_list = gp.PortInfoList()
    port_info_list.load()
    idx = port_info_list.lookup_path(camera_list[0][1])
    camera.set_port_info(port_info_list[idx])
    camera.init(self.context)

I've not tried this exact code (I adapted it from another project where I allow the user to choose a camera) but it should give you the idea.

from python-gphoto2.

flyte avatar flyte commented on June 24, 2024

So far I've narrowed the leak (if there's only one?) down to gp_port_info_list_load, as can be seen with the following:

import gphoto2 as gp

while True:
    pil = gp.check_result(gp.gp_port_info_list_new())
    gp.gp_port_info_list_load(pil)  # Leaks

and indeed just context.camera_autodetect() itself leaks, presumably because it calls gp_port_info_list_load:

import gphoto2 as gp

context = gp.gp_context_new()
while True:
    context.camera_autodetect()  # Leaks

Going further down the rabbit hole :)

from python-gphoto2.

flyte avatar flyte commented on June 24, 2024

I'm reaching the limit of my ability to track this down now. I'm not sure there's a way we can get around it without the issue being fixed upstream. There's a bug report already at gphoto/libgphoto2#165.

I guess I'll just have to run my code in a bash while loop and exit when a camera is not found/disconnects :(

from python-gphoto2.

jim-easterbrook avatar jim-easterbrook commented on June 24, 2024

I agree, it's not something I can do anything about in the Python interface. Please reopen this bug if you disagree.

from python-gphoto2.

AntiSol avatar AntiSol commented on June 24, 2024

Hi Jim, me again. I'm seeing a similar issue to this one.

memleak.py:

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

while running this, if you do something like

while true; do ps -eo pid,args,rss | grep "python [Mm]emleak.py" ; sleep 1; done

You'll see rapidly increasing memory usage. Note that you probably don't want to run that python code for very long unless you like seeing your system start swapping like crazy.

I think this is probably an issue with the library that I'm not going to be able to workaround without debugging C (which is beyond my abilities), I think it might be related to gphoto/libgphoto2#165 and gphoto/libgphoto2#208. Would you agree? or is this a different issue you might be able to help me track down?

the device I'm working with has limited memory, so if I can't find a fix or workaround for this it pretty much kills my project. I'd appreciate any suggestions.

from python-gphoto2.

jim-easterbrook avatar jim-easterbrook commented on June 24, 2024

It's possible this is a bug in the Python interface, as it's not to do with the camera autodetect stuff this bug is about. The whole config tree system is a bit of a nightmare, with nodes allocated in C that may or may not get properly freed when the Python object is deleted. I'll investigate further.

from python-gphoto2.

jim-easterbrook avatar jim-easterbrook commented on June 24, 2024

I've established that this is a bug, and have opened #67 for it.

from python-gphoto2.

AntiSol avatar AntiSol commented on June 24, 2024

thanks, I appreciate it.

from python-gphoto2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.