3

I have a problem displaying my MKPinAnnotationView on the mapView in iOS. I get this error but i don't understand where the error comes from : "EXC_BAD_ACCESS". My code seems good :

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {

MKPinAnnotationView *pv = [[MKPinAnnotationView alloc] init];
[pv setPinColor:MKPinAnnotationColorGreen];
[pv setCanShowCallout:YES];
[pv setRightCalloutAccessoryView:[UIButton buttonWithType:UIButtonTypeDetailDisclosure]];

return pv;

}

When i set breakpoints to the application, here is my console display using gdb:

    continue
Current language:  auto; currently objective-c
Program received signal:  “EXC_BAD_ACCESS”.
(gdb) bt
#0  0x023d1dfe in -[MKPinAnnotationView _setPinType:] ()
#1  0x0000324c in -[iSouvenirView mapView:viewForAnnotation:] (self=0x761abe0, _cmd=0x24221fd, mapView=0x7613410, annotation=0x761d360) at /Users/m2sar/iSouvenir/iSouvenirView.m:125
#2  0x02398130 in -[MKAnnotationContainerView _addViewForAnnotation:] ()
#3  0x02392b2a in -[MKAnnotationContainerView _addViewsForAnnotations:animated:] ()
#4  0x0238e657 in -[MKAnnotationContainerView showAddedAnnotationsAnimated:] ()
#5  0x0236837c in -[MKMapView _showAddedAnnotationsAndRouteAnimated:] ()
#6  0x02367cc8 in -[MKMapView mapTileViewDidFinishRendering:] ()
#7  0x023e7ae0 in -[MKMapTileView _didFinishRendering] ()
#8  0x000471c9 in __NSFireTimer ()
#9  0x025cff73 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ ()
#10 0x025d15b4 in __CFRunLoopDoTimer ()
#11 0x0252ddd9 in __CFRunLoopRun ()
#12 0x0252d350 in CFRunLoopRunSpecific ()
#13 0x0252d271 in CFRunLoopRunInMode ()
#14 0x0326000c in GSEventRunModal ()
#15 0x032600d1 in GSEventRun ()
#16 0x002bfaf2 in UIApplicationMain ()
#17 0x00002388 in main (argc=1, argv=0xbffff070) at /Users/m2sar/iSouvenir/main.m:14
(gdb)

The error is in the second line of the code. Any suggestion?

Thanks for your help

Dimitri
  • 8,122
  • 19
  • 71
  • 128
  • Can you copy/paste the stack trace from the console? – Greg Mar 24 '11 at 15:38
  • Run your app with **Build and Debug**. When the crash happens, open up the the debugger console (Cmd-Shift-R) and type bt. Copy the backtrace (everything after you typed 'bt') from the debugger console. Edit your message here and paste the backtrace. – Kirby Todd Mar 24 '11 at 15:42

2 Answers2

5

You must init the annotation view using initWithAnnotation:reuseIdentifier:. For example:

MKPinAnnotationView *pv = [[[MKPinAnnotationView alloc] 
    initWithAnnotation:annotation reuseIdentifier:@"annot"] autorelease];

However, you should also take advantage of annotation view re-use by calling dequeueReusableAnnotationViewWithIdentifier first.

Edit:
In the docs for MKAnnotationView, the paragraph titled "Reusing Annotation Views" explains why you should use dequeueReusableAnnotationViewWithIdentifier. So the code for viewForAnnotation would look like this:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{
    static NSString *myAnnotationIdentifier = @"annot";

    MKPinAnnotationView *pv = (MKPinAnnotationView *)[mapView 
        dequeueReusableAnnotationViewWithIdentifier:myAnnotationIdentifier];
    if (!pv)
    {
        pv = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation 
                reuseIdentifier:myAnnotationIdentifier] autorelease];
        [pv setPinColor:MKPinAnnotationColorGreen];
        [pv setCanShowCallout:YES];
        [pv setRightCalloutAccessoryView:[UIButton buttonWithType:UIButtonTypeDetailDisclosure]];
    }
    else
    {
        //we're re-using an annotation view
        //update annotation property in case re-used view was for another  
        pv.annotation = annotation;
    }

    return pv;
}
  • Thanks for your help. What do you mean with dequeueReusableAnnotationViewWithIdentifier ? – Dimitri Mar 24 '11 at 15:53
  • Annotation views are like table view cells and viewForAnnotation is like cellForRowAtIndexPath. For better performance, annotation views can be re-used. I'll add details to the answer. –  Mar 24 '11 at 15:57
1

Try:

MKPinAnnotationView *pv = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
Kirby Todd
  • 11,254
  • 3
  • 32
  • 60