2015-05-11 113 views
0

我檢索從gphoto2一個JPEG,產生從數據吉奧流,然後從該流創建PIXBUF:的Python PyGObject 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 

這樣做的功能被附接到GTK的空閒事件使用GLib.idle_add(...)。它可以工作,但會泄漏內存。該過程的記憶使用率不斷攀升。即使構建pixbuf的行被註釋掉了,它也會泄漏,但是當構造流的行也被註釋掉時,它就會泄漏,所以看起來它是流自身正在泄漏。在構建pixbuf後添加stream.close()並沒有幫助。

在這裏釋放內存的正確方法是什麼?

回答

2

我不會說這是一個答案,如果有人知道這個問題的直接答案,我很樂意將其標記爲正確的答案,但是對於處於同一位置的其他人,這裏有一個解決方法:

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

這不再泄漏內存。