user1878326
user1878326

Reputation: 49

Callout button crashes app - MapKit

UICalloutView willRemoveSubview:]: message sent to deallocated instance;

This only happens when I tap the callout button, but not on the first and not on the 2nd tap, but from the 3rd tap. So I tap the custom AnnotationView, callout pops, thats good. I tap it again, callout pops, all good. I tap another one, boom crash with that message. It only happes if is set the right accesoryview to be a button.

One key aspect to keep in mind..only happens in iOS 6... (go figure).

I am really stuck on this one – some help would be appreciated.

if ([annotation isKindOfClass:[RE_Annotation class]])
{
    RE_Annotation *myAnnotation = (RE_Annotation *)annotation;

    static NSString *annotationIdentifier = @"annotationIdentifier";

    RE_AnnotationView *newAnnotationView = (RE_AnnotationView *)[mapViews dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];


    if(newAnnotationView)
    {
        newAnnotationView.annotation = myAnnotation;
    }
    else
    {
        newAnnotationView = [[RE_AnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:annotationIdentifier];
    }

    return newAnnotationView;
}
return nil;

Also, this is my initwithannotation method:

- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{

    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    if(self)
    {

    RE_Annotation *myAnnotation = annotation;

    self = [super initWithAnnotation:myAnnotation reuseIdentifier:reuseIdentifier];

    self.frame = CGRectMake(0, 0, kWidth, kHeight);

    self.backgroundColor = [UIColor clearColor];

    annotationView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"map_pin_pink.png"]];

    annotationView.frame = CGRectMake(0, 0, kWidth - 2 *kBorder, kHeight - 2 * kBorder);



    [self addSubview:annotationView];

    [annotationView setContentMode:UIViewContentModeScaleAspectFill];

    self.canShowCallout = YES;

     self.rightCalloutAccessoryView = [[UIButton buttonWithType:UIButtonTypeInfoLight] retain]; ///if i take it out it doesnt crash the app. if i leave it it says that message
    }

    return self ;

}

Upvotes: 1

Views: 366

Answers (1)

user467105
user467105

Reputation:

In the initWithAnnotation method, there is this line:

self.rightCalloutAccessoryView = 
    [[UIButton buttonWithType:UIButtonTypeInfoLight] retain]; 
///if i take it out it doesnt crash the app. if i leave it it says that message

By "it", you must be referring to the retain and this implies the app is not using ARC.


Based on that, you should make the following corrections:

  • In viewForAnnotation, you need to autorelease the view when you alloc+init it otherwise there's a leak:

    newAnnotationView = [[[RE_AnnotationView alloc] 
        initWithAnnotation:myAnnotation 
        reuseIdentifier:annotationIdentifier] autorelease];
    
  • In initWithAnnotation, remove the retain when creating the callout button since buttonWithType returns an auto-released object (and you don't want to over-retain it):

    self.rightCalloutAccessoryView = 
        [UIButton buttonWithType:UIButtonTypeInfoLight];
    


Another possibly unrelated issue is that in initWithAnnotation, the code is calling super initWithAnnotation twice. This seems unnecessary and may be harmful. Remove the second call.


The above changes at least fix the issues with the code shown. However, there may be other, similar memory-management related issues in the rest of the app that may still cause crashes. Check and solve all issues reported by Analyzer.

Regarding the fact that "it only happens in iOS 6": iOS 6 may be less forgiving of memory-management errors or an internal change in the SDK may be exposing or manifesting these errors earlier. Regardless, the above fixes are necessary.


An unrelated point is there should be no need to manually create a UIImageView and addSubview it to the annotation view. You can just set the annotation view's image property.

Upvotes: 1

Related Questions