在模拟器中崩溃但是 iphone 当我使用 dispatch_aplly

crash in simulator but iphone when I use dispatch_aplly

当我使用 dispatch_apply 将一些数据添加到 nscountset 时,我在模拟器中遇到崩溃,但是 iphone。并收到一条消息“-[__NSArrayI isEqual:]: message sent to deallocated instance 0x60000024c5a0”,我不知道它是怎么发生的。

CGSize thumbSize = CGSizeMake(200,200);   
NSCountedSet *cls2 = [NSCountedSet setWithCapacity:thumbSize.width * thumbSize.height];
dispatch_apply(thumbSize.width, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t index) {
    int x = (int)index;
    for (int y = 0; y < thumbSize.height; y++) {
        if(y<x){
            continue;
        }
        int offset = 4*(x*y);
        int red = data[offset];
        int green = data[offset+1];
        int blue = data[offset+2];
        int alpha = data[offset+3];
        NSArray *clr2 = @[@(red),@(green),@(blue),@(alpha)];
        [cls2 addObject:clr2];
    }

});

在 [cls2 addobject:clr2] 中崩溃;日志是“-[__NSArrayI isEqual:]:发送到已释放实例 0x60000024c5a0 的消息”。 我的函数中有所有代码,我想获得图像中的最多颜色。

CGSize thumbSize = CGSizeMake(200, 200);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGContextRef context = CGBitmapContextCreate(NULL, thumbSize.width, thumbSize.height, 8, thumbSize.width * 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

CGRect drawRect = CGRectMake(0, 0, thumbSize.width, thumbSize.height);

CGContextDrawImage(context, drawRect, self.CGImage);

CGColorSpaceRelease(colorSpace);
unsigned char *data = CGBitmapContextGetData(context);
if (data == NULL)
{
    return nil;
}
NSCountedSet *cls2 = [NSCountedSet setWithCapacity:thumbSize.width * thumbSize.height];
dispatch_apply(thumbSize.width, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t index) {
    int x = (int) index;
    for (int y = 0; y < thumbSize.height; y++)
    {

        if (y < x)
        {
            continue;
        }
        int offset = 4 * (x * y);
        int red = data[offset];
        int green = data[offset + 1];
        int blue = data[offset + 2];
        int alpha = data[offset + 3];
        NSArray *clr3 = @[@(red), @(green), @(blue), @(alpha)];
        [cls2 addObject:clr3];
    }

});

CGContextRelease(context);

NSEnumerator *enumerator = [cls2 objectEnumerator];
NSArray *curColor = nil;
NSArray *maxColor = nil;
NSUInteger maxCount = 0;
while ((curColor = [enumerator nextObject]) != nil)
{
    NSUInteger tmpCount = [cls2 countForObject:curColor];
    if (tmpCount < maxCount) {
        continue;
    }

    maxCount = tmpCount;
    maxColor = curColor;
}

NSLog(@"colors: RGB A %f %d %d  %d",[maxColor[0] floatValue],[maxColor[1] intValue],[maxColor[2] intValue],[maxColor[3] intValue]);

return [UIColor colorWithRed:([maxColor[0] intValue]/255.0f) green:([maxColor[1] intValue]/255.0f) blue:([maxColor[2] intValue]/255.0f) alpha:([maxColor[3] intValue]/255.0f)];

NSCountedSet 不是线程安全的 - reference。尝试从并发线程中改变一个线程会产生不可预知的结果 - 正如您所发现的。

使用不带 dispatch_apply 的简单 for 循环(如果需要,在后台线程上分派)来处理您的数据,或者通过在串行调度队列。