componentsSeparatedByString和stringWithFormat泄漏,有没有更好的方法呢?

时间:2010-09-10 18:13:18

标签: iphone objective-c memory memory-leaks

我在仪器中的这段代码中遇到了一堆内存泄漏。

下面:

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

和这里:

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

有更好的方法吗?我基本上是在解析字符串并将数据添加到数组中。我该如何清理它?

更新:CoordinateArray保留在.h中并以dealloc发布。应该早点提到。 完整代码:

-(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];   
}

1 个答案:

答案 0 :(得分:0)

您的Coordinate对象上的属性很可能被声明为retaincopy,并且您在坐标的release方法中忘记了dealloc。< / p>

泄漏工具显示泄漏对象的创建位置,而不是丢失的位置。