I'm retrieving a JPEG from gphoto2, creating a Gio stream from the data and then creating a Pixbuf from the stream:
import gphoto2 as gp
from gi.repository import Gio, GdkPixbuf
camera = gp.Camera()
context = gp.Context
camera.init(context)
file = gp.CameraFile()
camera.capture_preview(file, context)
data = memoryview(file.get_data_and_size())
stream = Gio.MemoryInputStream.new_from_data(data)
pixbuf = GtkPixbuf.Pixbuf.new_from_stream(stream)
# display pixbuf in GtkImage
The function doing this is attached to the Gtk idle event using GLib.idle_add(...)
. It works, but it leaks memory. The process's memory use climbs continually. It leaks even when the line constructing the pixbuf is commented out, but not when the line constructing the stream is also commented out, so it seems that it is the stream itself that is leaking. Adding stream.close()
after constructing the pixbuf doesn't help.
What's the right way to release the memory here?
I wouldn't call it an answer as such, and if someone knows a direct answer to the question I'm happy to mark it as the right answer, but here's a workaround for anyone else in the same position:
import gphoto2 as gp
from gi.repository import Gio, GdkPixbuf
camera = gp.Camera()
context = gp.Context
camera.init(context)
file = gp.CameraFile()
camera.capture_preview(file, context)
data = memoryview(file.get_data_and_size())
loader = GdkPixbuf.PixbufLoader.new()
loader.write(data)
pixbuf = loader.get_pixbuf()
# use the pixbuf
loader.close()
This no longer leaks memory.