假性陈述不会以圆弧释放

时间:2022-12-10 09:43:55

I have an OSX Cocoa ARC project that handles thousands of images. In order to reduce the number of images, we scan the images to see if they are a uniform color, and if so, we discard the image and keep track of its color.

我有一个OSX Cocoa ARC项目,可以处理数以千计的图片。为了减少图像的数量,我们对图像进行扫描,看看它们是否是统一的颜色,如果是统一的颜色,我们丢弃图像并跟踪其颜色。

The code to get a solid color is:

得到纯色的代码是:

- (NSColor *)getSolidColor:(NSImage *)image
{
NSBitmapImageRep* raw_img = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]];

int Bpr = [raw_img bytesPerRow],spp = [raw_img samplesPerPixel];
unsigned char *data = [raw_img bitmapData];
int w = [raw_img pixelsWide],h = [raw_img pixelsHigh];

uint32_t mask = 0x00FFFFFF;
uint32_t color = *((uint32_t *)data) & mask;

    for( int y=0; y<h; y++ ) 
    {
    unsigned char *p = data + Bpr*y;

        for( int x=0; x<w; x++ ) 
        {
            if( color != (*((uint32_t *)p) & mask) )
                return( nil );
            p += spp;
        }
    }

    return( [raw_img colorAtX:0 y:0] );
}

(Some error checking removed for brevity - above code assumes 3 samples per pixel.)

(为了简便起见,删除了一些错误检查——上面的代码假设每个像素有3个样本。)

The code that calls it is basically:

调用它的代码基本上是:

NSString *imageFile;
while( imageFile = [self getNextImageFile] )
{
NSImage *image = [[NSImage alloc] initWithContentsOfFile:imageFile];
    if( [image isValid] && [self getSolidColor:image] )
        [fileManager removeItemAtPath:imageFile error:nil];
}

The problem is that the application consumes an exraordinary amount of memory. When I run it in the profiler, it indicates that 90% of the memory used is being allocated by [NSImage TIFFRepresentation] (bolded above.)

问题是,应用程序消耗的内存非常大。当我在profiler中运行它时,它表明所使用的90%的内存都是由[NSImage占用表示法](在上面的粗体中)分配的。

i.e. The TIFFRepresentation data and NSBitmapImageRep are never freed even though they fall out of scope as soon as the function returns.

例如,紧包表示数据和NSBitmapImageRep不会被释放,即使它们在函数返回时就脱离了作用域。

Why? And what should/can I do to force ARC to release those blocks?

为什么?我该怎样做才能使弧线释放这些块呢?

With the 'old way' using non-ARC I would just put an autorelease pool inside the while loop for the images and that would take care of the problem. Is there such a concept with ARC?

使用非arc的“老方法”,我只需要在while循环中放入一个自动上传池,这样就可以解决这个问题。有这样一个有弧的概念吗?

Thanks.

谢谢。

(PS. NSZombies is NOT enabled.)

(PS. nszombie)未被启用。)

1 个解决方案

#1


4  

Autorelease pools are conceptually still available with ARC, you just can't use the NSAutoreleasePool class anymore.

在概念上,使用ARC仍然可以使用Autorelease池,您再也不能使用NSAutoreleasePool类了。

Use the new @autoreleasepool keyword instead:

使用新的@autoreleasepool关键字:

@autoreleasepool {
   //Do stuff that you previously would have wrapped in an NSAutoreleasePool...
}

#1


4  

Autorelease pools are conceptually still available with ARC, you just can't use the NSAutoreleasePool class anymore.

在概念上,使用ARC仍然可以使用Autorelease池,您再也不能使用NSAutoreleasePool类了。

Use the new @autoreleasepool keyword instead:

使用新的@autoreleasepool关键字:

@autoreleasepool {
   //Do stuff that you previously would have wrapped in an NSAutoreleasePool...
}