goldcoin goldcoin - 1 year ago 90
Objective-C Question

Memory leak pyobjc

I wrote fallowing code:

import array
import Cocoa
import Quartz
import Quartz.CoreGraphics as qcg

while True:
imageRect = qcg.CGRectMake(0, 0, 1280, 800)
imageRef = qcg.CGWindowListCreateImage(imageRect,
bitmap = Cocoa.NSBitmapImageRep.alloc()
a = array.array('L', [255]*4)
color = bitmap.getPixel_atX_y_(a, 50, 22)

I am getting huge memory leak. With every iteration it increases around 30 MB. It happens when python executes this line:

color = bitmap.getPixel_atX_y_(a, 50, 22)

In Objective-C documentation there are not warnings about such behaviour. How to prevent this leak?


a = array.array('L', [255]*4)
pool = Cocoa.NSAutoreleasePool.alloc().init()
color = bitmap.getPixel_atX_y_(a, 50, 22)
del pool

This stops the leak.

Answer Source

I also face the same problem of memory utilisation in my application as it reaches to 600 mb then my app crashes,then i release the memory manually by using an autorelease pool .

 @autoreleasepool {       

//Write Your Code Here


Create an autorelease pool and then check your memory utilisation .Hope this Helps you