如何在 NSOperation 中阻止 NSImage 锁定焦点泄漏内存?
当我使用锁定/解锁焦点绘制 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 每次调用时都会分配新的空间。
I have a problem with NSImages leaking memory when I draw to them with lock/unlockfocus. The leak goes away 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了几乎相同的问题,需要添加一个自动释放池。
非 ARC:
ARC:
I had a nearly identical issue and needed to add an autorelease pool.
Non-ARC:
ARC:
好吧,我仍然不完全确定如何完全阻止泄漏,但我大大减少了锁定焦点/解锁焦点的次数。这基本上解决了我的问题。
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.
我会看看您的
-getColors
和-getAudioWaveColor:
方法。I'd look at your
-getColors
and-getAudioWaveColor:
methods.