Python Pygobject PixBuf内存泄漏



我正在从gphoto2中检索JPEG,从数据创建GIO流,然后从流中创建PixBuf:

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

使用GLib.idle_add(...)将执行此操作的功能附加到GTK空闲事件。它有效,但会泄漏内存。该过程的记忆使用不断攀登。即使在构造PixBuf的行进行评论时,它也会泄漏,但是当构造流的线路构造流程时,它也不会发表评论,因此似乎是流本身正在泄漏。构建PixBuf后添加stream.close()无济于事。

在此处释放内存的正确方法是什么?

我不会这样称呼它,如果有人知道对问题的直接答案,我很乐意将其标记为正确的答案,但是对于任何人来说,否则处于同一位置:

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()

这不再泄漏内存。

最新更新