objective-c - 如何阻止 NSImage lockfocus 在 NSOperation 中泄漏内存?

标签 objective-c memory-leaks osx-lion 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 每次调用时都在分配新空间。

最佳答案

我有一个几乎相同的问题,需要添加一个自动释放池。

非 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];

圆弧:

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

关于objective-c - 如何阻止 NSImage lockfocus 在 NSOperation 中泄漏内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7099722/

相关文章:

ios - 使用 2 个链接的 ManagedObjectContext 应用程序撤消 CoreData 更改

ios - 将数组过滤成部分

ios - UIDatePickers 高度没有动画?

ios - 带有音频 AVAssetWriterInput "leaks"内存的 appendSampleBuffer 直到 endSessionAtSourceTime

javascript - 在 Safari 中禁用 flex 滚动

python - OS X Lion 中的 easy_install pip 需要 sudo 吗?

ios - 使用动画在 UITextField 中移动文本

c++ - 一个奇怪的内存泄漏问题

list - 为什么 `list.Remove()` 试图明确避免内存泄漏?

ruby-on-rails - 无法构建 gem native 扩展