Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDictionaryM setObject:forKey: EXC_BAD_ACCESS crash

Crashlog:

Crashed: com.apple.main-thread
EXC_BAD_ACCESS KERN_INVALID_ADDRESS at 0x12121212

libobjc.A.dylib - objc_msgSend - isEqual:
CoreFoundation  - -[__NSDictionaryM setObject:forKey:]

Code:

if (object) {
    [_objects setObject:object forKey:key];
}

When init of _objects happens earlier in code:

- (id)init {
     self = [super init];
     if (self) {
         _objects = [[NSMutableDictionary alloc] init];
     }
     return self;
}

Object can be nil value, thats why I'm checking if it exist:

AFHTTPRequestOperation *object = [[AFHTTPRequestOperation alloc] initWithRequest:request];

I'm not sure what exactly can cause it. Crash happens very rarely, I'm unable to reproduce it, however a few users got it. I assume something got deallocated, could it be that _objects is nil? Or whole controller got deallocated? Why I can see isEqual in the log?

Can this fix the issue?

if (object && _objects) {
    [_objects setObject:object forKey:key];
}
like image 294
Nat Avatar asked Mar 26 '14 07:03

Nat


1 Answers

You have checked not null condition for the dictionary and object value. Try to check nil condition for your key of object. There might be a case where key can be nil.

if (object && _objects && key) {
    [_objects setObject:object forKey:key];
}
like image 90
Pooja M. Bohora Avatar answered Oct 18 '22 12:10

Pooja M. Bohora