So I have a Navigation Controller, I push a view controller on it, then a second. In the second I start listing for location updates. Ok fine.
When I click back button on the second view controller, I'm sometimes getting "*** -[DistanceScreen respondsToSelector:]: message sent to deallocated instance 0x1152a0"
Note testing on actual device (iPhone 4, 4.2 iOS).
And stack looks like:
#0 0x33a69910 in ___forwarding___
#1 0x33a69860 in __forwarding_prep_0___
#2 0x343713fa in -[CLLocationManager onClientEventLocation:]
#3 0x3436f694 in -[CLLocationManager onClientEvent:supportInfo:]
#4 0x3436f80a in OnClientEvent
#5 0x3436b528 in CLClientInvokeCallback
#6 0x3436d3d2 in CLClientHandleDaemonDataLocation
#7 0x3436d518 in CLClientHandleDaemonData
#8 0x33a81404 in __CFMessagePortPerform
#9 0x33a556fe in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__
#10 0x33a556c2 in __CFRunLoopDoSource1
#11 0x33a47f7c in __CFRunLoopRun
#12 0x33a47c86 in CFRunLoopRunSpecific
#13 0x33a47b8e in CFRunLoopRunInMode
#14 0x33b0e4aa in GSEventRunModal
#15 0x33b0e556 in GSEventRun
#16 0x32099328 in -[UIApplication _run]
#17 0x32096e92 in UIApplicationMain
#18 0x0000280e in main at main.m:13
Where "DistanceScreen" above is the view controller that started listening for location updates. And before you respond, here is the dealloc for DistanceScreen:
NSLog(@"DistanceScreen dealloc");
[locationManager setDelegate:nil];
[locationManager stopUpdatingLocation];
[locationManager release];
//etc.
[super dealloc];
So if I'm setting the delegate to nil, why does it look like Location Manager is still trying to send the update? Or is it because CLLocationManager is doing stuff in its own thread, and started the location update process just as the delegate was deallocated???? In which case there is no easy safe way to stop listening for updates.
I had this same problem testing my app on 4.3, but not in 5.0. The issue was that I was trying to dealloc the CLLocationManager inside one of the delegate methods. It works fine in 5.0, but crashes a iPhone 3GS running 4.3 every time.
In the end I made the CLLocationManager object a property, then in the delegate method set the delegate to nil and dispatched a block to the main thread to release the object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With