views:

2031

answers:

5

Ok, so I’m having this problem. What I want to do is manually add multiple annotations to a map. When I add just one annotation, it works flawlessly. The pin drops, you can click on it to see its callout, life is good.

The problem comes when I want to add more than one. When I add the second, suddenly the pin’s aren’t coloured correctly (i.e. depending on their magnitude they should be a certain color, but they’re now both the same…), and more importantly when you click on them, to see their callout, the app crashes with exex_bad_access. I really have no idea what’s wrong, maybe I’m adding too many views to the map? But it’s only 9 pins and the pins themselves add just fine. Here’s my code…

    - (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableArray *stops = [[NSMutableArray alloc] init];  //Get list of all the stops available
    Bus *bus1 = [[Bus alloc] init];                         // Bus 1 holds the stops
    stops = [bus1 returnStops];
    for (NSString *stop in stops)                           //Go through each stop to add annotation to map
    {
        Bus *bus2 = [bus1 initWithStop:stop];                //Create an instance of bus with a given stop
        MapAnnotation *eqAnn = [MapAnnotation annotationWithBus:bus2]; 
        [self.mapView addAnnotation:eqAnn];                    //Add the annotation to the map
        //[eqAnn release];
        //[bus2 release];
    }
    [self recenterMap];
    [stops release];

}
- (MKAnnotationView *)mapView:(MKMapView *)mapView 
            viewForAnnotation:(id <MKAnnotation>)annotation {
    MKAnnotationView *view = nil;
    if(annotation != mapView.userLocation) {
        MapAnnotation *eqAnn = (MapAnnotation*)annotation;
        view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"busLoc"];
        if(nil == view) {
            view = [[[MKPinAnnotationView alloc] initWithAnnotation:eqAnn
                                                    reuseIdentifier:@"busLoc"] autorelease];
        }
        CGFloat magnituide = [eqAnn.bus.magnitude floatValue];

        if(magnituide >= .80f) {
            [(MKPinAnnotationView *)view setPinColor:MKPinAnnotationColorRed];
        } else if(magnituide >= .60f) {
            [(MKPinAnnotationView *)view setPinColor:MKPinAnnotationColorPurple];
        } else 
        {
            [(MKPinAnnotationView *)view setPinColor:MKPinAnnotationColorGreen];
        }
        [(MKPinAnnotationView *)view setAnimatesDrop:YES];
        [view setCanShowCallout:YES];
    } 

    return view;
}

even tried removing the second function, but it didn’t do anything.

Thanks for the help! P.S I should also add, there’s usually one or two pins out of the 9 which works when you click the annotation…

If i even try to manually just two annotations by hand in the program (i.e., remove the loop), it still fails and the color is still wrong...

A: 

It would appear that your memory management of the stops variable is incorrect. You allocate a mutable array, then replace that array with the return value of -[Bus returnStops], then release that. Also it's not clear what's going on with bus2 - does -[Bus initWithStop:] return a different instance of Bus? It's not usual to send any method -init* on an already-initialised object. I think that you probably are confused by the memory management conventions in Cocoa Touch. Here's a collection of articles and other references on Cocoa memory management (which is the same beast).

Graham Lee
A: 

Have you tried using AddAnnotations instead of add annotation? - (void)addAnnotations:(NSArray *)annotations. This might work for you...but looking at the answer above and further inspection you are having some memory managment issues in your viewDidLoad (though thi s might not be the cause of your problem, but it could be). First of you are allocating the array (stops) and then ovveriding it with some array in the Bus object, this will cause a leak. Also you are then releasing that array which might be causing the crash since you are releasing the array that is actually in the Bus object w ithout having increased a reference count to it. I am not sure what initWithStop is doing but you might be getting a leak here too if initWithStop retains the object.

Daniel
A: 

I wouldn't call it a memory management problem -- I'd just say you are using array references incorrectly.

After constructing the array with NSMutableArray *stops = [[NSMutableArray alloc] init], the next step is to use [stops addObject: ] to add each stop you want to store.

After that? It's not clear what you are really trying to do.

Amagrammer
A: 

SO the answer was that I kept sending bus1 the init object, so it got confused.

"Hi David,

Your data model looks hosed to me. You only have one bus object that you are repeatedly sending initWithStop: to.

Hope this helps.

Good luck! "

Thank you guys for your help! You all helped me quite a bit!

kodai
A: 

HOw did you solved the problem? Can you please send me the code for viewDidLoad method. I am facing same problem.

Amarpreet