문제

if I do this

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

my allColors mutable array has valid contents, but if I create a GGC group and do this...

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);

am I missing something?

thanks.

도움이 되었습니까?

해결책

You are creating an autoreleased array and the autorelease pool is being drained by GCD sometime between when the first block executes and the second block executes.

Any time you are doing concurrent programming, whether by thread or using GCD, you must always hard retain any object that is to survive beyond one scope of execution.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top