views:

82

answers:

2

I'm getting a bunch of memory leaks for this bit of code in Instruments.

Here:

NSArray *tmpCoords = [loc.mapCoords componentsSeparatedByString:@","];

and Here:

    coords.tileRow = [NSString stringWithFormat:@"%d",x];
    coords.tileCol = [NSString stringWithFormat:@"%d",y];

Is there a better way to do this? I'm basically parsing a string and adding stuff to an array. How do I clean this up?

Update: CoordinateArray is retained in .h and released in dealloc. Should have mentioned that earlier. Full Code:

-(void)loadCoordinates
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];  
    // get list of coordinates once
    coordinateArray = [[NSMutableArray alloc] init];

    MyAppDelegate *app = [[UIApplication sharedApplication] delegate];
    MyData *myData = app.data;

    NSArray *myArray = [myData locations];

    for ( NSUInteger i = 0; i < [myArray count]; i++ )
    {
        LocationModel *loc = [myArray objectAtIndex:i];
        NSArray *tmpCoords = [loc.mapCoords componentsSeparatedByString:@","];
        if ([tmpCoords count] < 2 ) continue;
        CoordinateModel *coords = [[CoordinateModel alloc] init];

        coords.row = [tmpCoords objectAtIndex:0];
        coords.col = [tmpCoords objectAtIndex:1];

        NSString *xString = [tmpCoords objectAtIndex:0];
        int x = [xString floatValue] / DEFAULT_TILE_SIZE;
        NSString *yString = [tmpCoords objectAtIndex:1];
        int y = [yString floatValue] / DEFAULT_TILE_SIZE;

        coords.tileRow = [NSString stringWithFormat:@"%d",x];
        coords.tileCol = [NSString stringWithFormat:@"%d",y];

        [coordinateArray addObject:coords];
        [coords release];

    }
    [pool release];   
}
A: 

Most likely the properties on your Coordinate object are declared as either retain or copy, and you're forgetting to release them in your Coordinate's dealloc method.

The leaks instrument shows where the leaked object was created, not where it was lost.

Dave DeLong
Dave, good comment. However, coordinateArray is retain in .h and released in dealloc. Instruments does trace back to the two objects (componentsSeparatedByString, stringWithFormat). I admit, I don't see anything glaringly obvious either. Jordan
Jordan
A: 

Didn't find an answer, though I did want to close the question.

Jordan