【问题标题】:Objective-C NSImage, memory leak (any advice) I am using ARCObjective-C NSImage,内存泄漏(任何建议)我正在使用 ARC
【发布时间】:2012-12-07 08:57:29
【问题描述】:

如何解决此内存泄漏问题?我有 NSBezierPath 的集合吗?如何解决此内存泄漏?有什么建议吗?我正在使用 ARC。

int main(int argc, const char * argv[])
{
    @autoreleasepool {

        // insert code here...
        for(int j=0;j<5000;j++){
            NSLog(@"Hello, World!");
            NSSize imageSize = NSMakeSize(512, 512);
            NSImage *image = [[NSImage alloc] initWithSize:imageSize];
            //draw a line:
            for(int i=0;i<1000;i++){
                [image lockFocus];
                float r1 = (float)(arc4random() % 500);
                float r2 = (float)(arc4random() % 500);
                float r3 = (float)(arc4random() % 500);
                float r4 = (float)(arc4random() % 500);
                [NSBezierPath strokeLineFromPoint:NSMakePoint(r1, r2) toPoint:NSMakePoint(r3, r4)];
            }
            //...

            NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0, 0, imageSize.width, imageSize.height)];
            NSData *pngData = [imageRep representationUsingType:NSPNGFileType properties:nil];
            [image unlockFocus];

            NSString *jstring = [NSString stringWithFormat:@"%d", j];
            jstring = [jstring stringByAppendingString:@".png"];
            [pngData writeToFile:jstring atomically:YES];
        }
    }

    return 0;
}

【问题讨论】:

  • 你说得对,我是个小新人。我喜欢只制作 5000 张带有随机线条的图片。

标签: objective-c macos memory-leaks


【解决方案1】:

修改后的答案,现在我知道您正在使用 ARC:

这可能不是您在泄漏,而是您的自动释放池随着循环的每次迭代而变得越来越大,因为在您的循环完成之前自动释放池不会清空。

您需要做的是在循环中使用辅助自动释放池。这是您的代码的修订版本来说明这一点。

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        // insert code here...
        for(int j=0; j<5000; j++) {
            NSLog(@"Hello, World!");
            NSSize imageSize = NSMakeSize(512, 512);
            @autoreleasepool {
                NSImage *image = [[NSImage alloc] initWithSize:imageSize];
                //draw a line:
                NSData *pngData;
                for(int i=0;i<1000;i++){
                    [image lockFocus];
                    float r1 = (float)(arc4random() % 500);
                    float r2 = (float)(arc4random() % 500);
                    float r3 = (float)(arc4random() % 500);
                    float r4 = (float)(arc4random() % 500);
                    [NSBezierPath strokeLineFromPoint:NSMakePoint(r1, r2) toPoint:NSMakePoint(r3, r4)];
                    //...

                    NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0, 0, imageSize.width, imageSize.height)];
                    pngData = [imageRep representationUsingType:NSPNGFileType properties:nil];
                    [image unlockFocus];
                }

                NSString *jstring = [NSString stringWithFormat:@"%d", j];
                jstring = [jstring stringByAppendingString:@".png"];
                [pngData writeToFile:jstring atomically:YES];
            }
        }
    }

    return 0;
}

不过,您似乎还有另一个问题。您是否正在为文件构建完整路径?

【讨论】:

  • @user1735714 查看我的编辑。 pngData 需要在循环范围之外声明。
  • @user1735714 您确实需要具体说明您更改了哪些导致这些错误的步骤,错误消息的确切内容,编译器抱怨的哪一行(针对编译器错误)等。当他们所拥有的关于您所看到的问题的信息模糊、不完整时,人们将无能为力。
  • @user1735714 再编辑一次。在循环内移动了您的 [image lockFocus]; 行。
  • 随着代码越来越长,节越来越明确,为什么不使用方法呢? OP没有的事实并不意味着你不应该。 @user1735714 这是否解决了问题?
  • @user1735714 最后看一下我编辑的答案;我能够自己运行它。
猜你喜欢
  • 2013-05-23
  • 1970-01-01
  • 2015-06-20
  • 1970-01-01
  • 1970-01-01
  • 2020-06-14
  • 1970-01-01
  • 2014-09-21
  • 2011-06-07
相关资源
最近更新 更多