iPhone - Grand Central Dispatch invalid mutable array
-
25-10-2019 - |
문제
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.
제휴하지 않습니다 StackOverflow