Why did my application crash when assigning itself to __weak local in ARC?

Crashlytics reported this failure:

0 libobjc.A.dylib _objc_trap() + 18446744073709552000 1 libobjc.A.dylib _objc_fatal + 71 2 libobjc.A.dylib append_referrer_no_lock(weak_referrer_array_t*, objc_object**) 3 libobjc.A.dylib objc_storeWeak + 120 4 MyApp CloudSyncButton.m line 58 -[CloudSyncButton observeValueForKeyPath:ofObject:change:context:] 5 .... 

The code in question:

 -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { CloudSyncButton* __weak weakSelf = self; //<---crashed here if([keyPath isEqualToString:kCloudSyncingKVO]) { dispatch_async(dispatch_get_main_queue(), ^{ CloudSyncButton* localSelf = weakSelf; [localSelf refreshCloudSyncIcon]; }); } } 

I need help to understand why this happened and what I can do to avoid this in the future. This is the first time I've seen something like this crash, so I wonder if this is an accident?

+7
source share
1 answer

Make sure that in all cases CloudSyncButton has removed itself from observing other objects in the dealloc method. It looks like this message is sent after your button has been released.

+7
source

All Articles