iosios8unrecognized-selectordidreceivememorywarning

Why is iOS sending memory warning to deallocated objects, causing my app to crash?


I was searching crash logs of my app, and I've seen this (which occured quiet a few times, not just once or twice):

enter image description here

As seen above, notification center posted a memory warning notification, and it was somehow forwarded to a CALayer. I've also seen instances of didReceiveMemoryWarning: messages sent to other deallocated objects such as UIImageViews or even private _UINavigationBarBackground objects, when zombies were enabled on my debugger, too, crashing my app. Why would this happen?


Solution

  • NSNotificationCenter only keeps weak references to observers.

    What's probably happening is that you have one ore more objects somewhere that register for UIApplicationDidReceiveMemoryWarningNotification, but which never unregister (thats a bug). Since NSNotificationCenter only keeps weak references to those objects it doesn't notice when they are dealloc'd and their memory is reused for other objects such as CALayer etc, which do not implement a method named didReceiveMemoryWarning.