Search code examples
pythonobjective-cmacosmemory-leakspyobjc

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

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?

EDIT:

.
.
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.


Solution

  • 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