0
我寫休耕代碼:內存泄漏pyobjc
import array
import Cocoa
import Quartz
import Quartz.CoreGraphics as qcg
while True:
imageRect = qcg.CGRectMake(0, 0, 1280, 800)
imageRef = qcg.CGWindowListCreateImage(imageRect,
qcg.kCGWindowListOptionOnScreenOnly,
qcg.kCGNullWindowID,
qcg.kCGWindowImageDefault)
bitmap = Cocoa.NSBitmapImageRep.alloc()
bitmap.initWithCGImage_(imageRef)
a = array.array('L', [255]*4)
color = bitmap.getPixel_atX_y_(a, 50, 22)
time.sleep(1)
我越來越嚴重的內存泄露。隨着每次迭代它增加大約30 MB。它發生時,蟒蛇執行這條線:
color = bitmap.getPixel_atX_y_(a, 50, 22)
在Objective-C的文檔有不是這樣的行爲的警告。如何防止這種泄漏?
編輯:
.
.
a = array.array('L', [255]*4)
pool = Cocoa.NSAutoreleasePool.alloc().init()
color = bitmap.getPixel_atX_y_(a, 50, 22)
del pool
.
.
這將停止泄漏。
以上,我寫的蟒蛇。謝謝! – goldcoin