当我使用lock / unlockfocus绘制NSImages时,我遇到了NSImages泄漏内存的问题。当我在下面注释LEAKS HERE代码时泄漏消失了。所以我知道这就是发生泄漏的地方。
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
}
我正在使用垃圾收集,这个for循环是在OSX 10.7 Lion的NSOperationQueue中运行的NSOperation的一部分。
这是NSImage在后台线程/操作上的锁定焦点的错误吗?
编辑: 似乎lockFocus每次调用时都会分配新的空间。
答案 0 :(得分:0)
我会查看您的-getColors
和-getAudioWaveColor:
方法。
答案 1 :(得分:0)
嗯,我仍然不完全确定如何完全阻止泄漏,但是我大大减少了lockFocus / unlockFocus的次数。这基本上解决了我的问题。
答案 2 :(得分:0)
我有一个几乎相同的问题,需要添加一个自动释放池。
非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];
}