当我使用锁定/解锁焦点绘制它们时,我遇到了 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/

10-10 20:32
查看更多