开发者

Leaks on componentsSeparatedByString and stringWithFormat, is there a better way to do this?

开发者 https://www.devze.com 2023-01-15 10:17 出处:网络
I\'m getting a bunch of memory leaks for this bit of code in Instruments. Here: NSArray *tmpCoords = [loc.mapCoords componentsSeparatedByString:@\",\"];

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];
        co开发者_StackOverflowords.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];   
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消