【发布时间】:2011-10-29 07:23:10
【问题描述】:
当我使用锁定/解锁焦点绘制到 NSImage 时,我遇到了内存泄漏的问题。当我注释掉下面的 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 每次调用时都在分配新空间。
【问题讨论】:
-
你发布过图片吗?
-
嗨查克,感谢您的评论。我没有直接释放图像,因为我使用的是垃圾收集 (gc),但在调用上面的代码之前,我确实将 NSImageArray 中的每个元素设置为 nil,它使用 gc 信号收集。
-
我尝试不在 for 循环中创建新的 NSImage。相反,我只制作了一次,但后来我无法清除 NSImageRep 缓存,并且 lockFocus unlockfocus 代码仍然以相同的速度泄漏。
标签: objective-c memory-leaks osx-lion nsoperation nsimage