tallybear
tallybear

Reputation: 417

NSMutableArray seems to be prematurely releasing

I'm trying to add Annotations to an array to place multiple pins on a map. I have everything in a for loop. The first time it loops through, it adds the object to the array just fine. When it goes back through... the array has 0 objects in it. Can anyone tell me why?

EDIT: I'm using ARC.

-(void)plotMultipleLocs {
float latitude;
float longitude;
NSRange commaIndex;
NSString *coordGroups;
for (int i=0; i<=cgIdArray.count; i++) {
    coordGroups = [cgInAreaArray objectAtIndex:i];
    commaIndex = [coordGroups rangeOfString:@","];
    latitude = [[coordGroups substringToIndex:commaIndex.location] floatValue];
    longitude = [[coordGroups substringFromIndex:commaIndex.location + 1] floatValue];
    CLLocationCoordinate2D loc = CLLocationCoordinate2DMake(latitude, longitude);
    MKCoordinateRegion reg = MKCoordinateRegionMakeWithDistance(loc, 1000, 1000);
    self->mapView.region = reg;
    MKPointAnnotation* ann = [[MKPointAnnotation alloc] init];
    ann.coordinate = loc;
    ann.title = [cgNameArray objectAtIndex:i];
    ann.subtitle = [cgLocArray objectAtIndex:i];
    NSMutableArray *mutAnnArray = [NSMutableArray arrayWithArray:annArray];
    [mutAnnArray addObject:ann];
 }
}

Upvotes: 0

Views: 269

Answers (3)

user23743
user23743

Reputation:

Every time through the loop, you create a new mutable array with the contents of a different array. The mutable array containing the object you added on the previous iteration is not kept.

Upvotes: 0

Abizern
Abizern

Reputation: 150615

You are creating a mutable array within the loop and adding your object to it.

At the next iteration of the loop, you create a new mutable array and add a new annotation to it.

Leave aside the fact that you are creating it from another array rather than just adding your annotation to annArray

Basically, the array that you are adding objects to last as long as one iteration, and then goes out of scope.

Try moving the array out of the loop:

-(void)plotMultipleLocs {
    float latitude;
    float longitude;
    NSRange commaIndex;
    NSString *coordGroups;

    NSMutableArray *mutAnnArray = [NSMutableArray arrayWithArray:annArray]; // Create one array outside the loop.

    for (int i=0; i<=cgIdArray.count; i++) {
        coordGroups = [cgInAreaArray objectAtIndex:i];
        commaIndex = [coordGroups rangeOfString:@","];
        latitude = [[coordGroups substringToIndex:commaIndex.location] floatValue];
        longitude = [[coordGroups substringFromIndex:commaIndex.location + 1] floatValue];
        CLLocationCoordinate2D loc = CLLocationCoordinate2DMake(latitude, longitude);
         MKCoordinateRegion reg = MKCoordinateRegionMakeWithDistance(loc, 1000, 1000);
         self->mapView.region = reg;
         MKPointAnnotation* ann = [[MKPointAnnotation alloc] init];
         ann.coordinate = loc;
         ann.title = [cgNameArray objectAtIndex:i];
         ann.subtitle = [cgLocArray objectAtIndex:i];
        [mutAnnArray addObject:ann]; // Add the annotation to the single array.
    }

// mutAnnArray will go out of scope here, so maybe return it, or assign it to a property
}

Upvotes: 4

Joel Martinez
Joel Martinez

Reputation: 47749

Have you tried retaining the instance to avoid it being released?

Upvotes: 0

Related Questions