开发者

How to stop NSImage lockfocus from Leaking Memory in an NSOperation?

开发者 https://www.devze.com 2023-03-28 16:16 出处:网络
I have a problem with NSImages leaking memory when I draw to them with lock/unlockfocus. The leak goes awa开发者_开发技巧y when I comment out the LEAKS HERE code below. So I know that is where the lea

I have a problem with NSImages leaking memory when I draw to them with lock/unlockfocus. The leak goes awa开发者_开发技巧y when I comment out the LEAKS HERE code below. So I know that is where the leak is happening.

for(int i= 0; i < nNumberImages; ++i)
{
    m_apNSImageArray[i]= [[NSImage alloc] initWithSize:m_viewRect.size];        
    if(!m_apNSImageArray[i])
    {
        return;
    }  

    //LEAKS IN THIS CODE HERE
    [m_apNSImageArray[i] lockFocus];

    //EDIT: Commented the lines below out, but leak persists.    
    //[[[[NSApp delegate] getColors] getAudioWaveColor:YES] setStroke];        
    //[[m_pmaBezierPaths objectAtIndex:i] stroke];    

    [m_apNSImageArray[i] unlockFocus];      
    //TO HERE        
}

I'm using garbage collection, and this for-loop is part of an NSOperation running in an NSOperationQueue in OSX 10.7 Lion.

Is this a bug with NSImage's lockfocus on background threads/operations?

EDIT: It appears that lockFocus is allocating new space each time its called.


I had a nearly identical issue and needed to add an autorelease pool.

Non-ARC:

// set up the autorelease pool
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

// do image stuff
NSImage *imagemage = [[NSImage alloc] init];
[maskedImage lockFocus];
[maskedImage unlockFocus];
[image release];

// drain the autorelease pool
[pool drain];

ARC:

@autoreleasepool {
    NSImage *imagemage = [[NSImage alloc] init];
    [maskedImage lockFocus];
    [maskedImage unlockFocus];
}


Well I still am not totally sure how to stop the leak completely, but I drastically reduced the number of times I lockFocus/unlockFocus. This essentially solved my problem.


I'd look at your -getColors and -getAudioWaveColor: methods.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号