我在仪器中的这段代码中遇到了一堆内存泄漏。
下面:
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];
}
答案 0 :(得分:0)
您的Coordinate对象上的属性很可能被声明为retain
或copy
,并且您在坐标的release
方法中忘记了dealloc
。< / p>
泄漏工具显示泄漏对象的创建位置,而不是丢失的位置。