Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Raztor0/08e09b987d6e5b08da7d754e4f5d5d4c to your computer and use it in GitHub Desktop.
Save Raztor0/08e09b987d6e5b08da7d754e4f5d5d4c to your computer and use it in GitHub Desktop.
@interface SomeClass : NSObject
@property (nonatomic, strong) NSDictionary *someDictionary;
@end
@implementation SomeClass
+ (void)load {
[self new];
}
- (instancetype)init {
self = [super init];
if (self) {
NSLock *lock;
lock = [NSLock new];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
NSLog(@"%@", [NSThread currentThread]);
while (true) {
@autoreleasepool {
int r = arc4random_uniform(2);
if (r % 2 == 0) {
self.someDictionary = nil;
} else {
NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
for (int i = 0; i < 100; i++) {
[mutableDict setObject:@"hi" forKey:[[NSProcessInfo processInfo] globallyUniqueString]];
}
[lock lock];
self.someDictionary = mutableDict;
[lock unlock];
}
}
}
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
NSLog(@"%@", [NSThread currentThread]);
while (true) {
@autoreleasepool {
[lock lock];
NSDictionary *myDict = self.someDictionary;
/* This line will throw the exception:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'
*/
NSArray __unused *keys = myDict.allKeys;
[lock unlock];
}
}
});
}
return self;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment