iPhone - Grand Central Dispatch无效的可变阵列

时间:2011-07-24 17:51:51

标签: iphone grand-central-dispatch

如果我这样做

NSMutableArray *allColors;
NSData *dataColor = [dictPLIST objectForKey:@"allColors"];
if (dataColor != nil)   {
    allColors = [NSMutableArray arrayWithArray:
      [NSKeyedUnarchiver unarchiveObjectWithData:dataColor]];
}
dataColor = nil;

我的allColors可变数组有有效内容,但如果我创建一个GGC组并执行此操作...

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_group_t group = dispatch_group_create();

__block NSMutableArray *allColors;
dispatch_group_async(group, queue, ^{
    NSData *dataColor = [dictPLIST objectForKey:@"allColors"];
    if (dataColor != nil)   {
        allColors = [NSMutableArray arrayWithArray:
              [NSKeyedUnarchiver unarchiveObjectWithData:dataColor]];
    }
    dataColor = nil;    
});

// .... other stuff is added to the group


dispatch_group_notify(group, queue, ^{
   dispatch_group_async(group, queue, ^{

    // if I try to access allColors here, the app crashes

   });

});

dispatch_release(group);
我错过了什么吗?

感谢。

1 个答案:

答案 0 :(得分:2)

您正在创建一个自动释放的数组,并且在第一个块执行和第二个块执行之间的某个时间GCD正在耗尽自动释放池。

无论是通过线程还是使用GCD,无论何时进行并发编程,都必须始终坚持保留任何超出一个执行范围的对象