Question

I get a crash in my app with the message " ... sent to deallocated instance at the address ...". So, I analyzed the app with zombie instruments and I've presented below a fragment of code which causes the crash. I haven't found yet a scenario which causes this error.

(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
if ([view.annotation isKindOfClass:[MKUserLocation class]]) {
}
else {
    CustomAnnotation *ann = (CustomAnnotation *) view.annotation;
    if (ann.annotationType == BusAnnotationType) {
        NSLog(@"accessory button tapped for annotation %@", view.annotation);
        BusInfoViewController *viewController = [[BusInfoViewController alloc] initWithNibName:@"BusInfoViewController" bundle:nil];
        BusForStation *bus = [self getBusWithId:[(CustomAnnotation *)view.annotation ID]];
        viewController.currentBus = bus;
        [self.navigationController pushViewController:viewController animated:YES];
        [viewController release];
    }
}

The analyzer tool gets 91.4% at the line [self.navigationController pushViewController:viewController animated:YES];

Does anyone know what might be the problem?

Was it helpful?

Solution

Consider using ARC (Automatic Reference Counting). Xcode can convert your project almost completely automatically with Menu "Edit" ➞ "Refactor" ➞ "Convert to Objective-C ARC…". There are only few reasons to keep managing memory manually.

You could also try running the static analyzer (Menu "Product" ➞ "Analyze").

OTHER TIPS

I think I solved a similar problem without converting to ARC. I have a navigationController with my viewController containing a mapView. When the view is loaded, I call setRegion:myregion animated:YES. If I click "Back" before the animation is complete, the mapView raises a "message sent to deallocated instance" error on the [respondsToSelector:] message. I fixed this by setting myMapView.delegate = nil before releasing myMapView.

I think the problem you're having is that you're releasing the viewController straight after pushing it onto the view. Also if you've done your own implementation for setCurrentBus (in BusInfoViewController) the problem could be there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top