Iphone 通过String和stringWithFormat分离的组件泄漏,有更好的方法吗?
我在仪器中的这段代码有很多内存泄漏 在这里: 在这里:Iphone 通过String和stringWithFormat分离的组件泄漏,有更好的方法吗?,iphone,objective-c,memory,memory-leaks,Iphone,Objective C,Memory,Memory Leaks,我在仪器中的这段代码有很多内存泄漏 在这里: 在这里: coords.tileRow = [NSString stringWithFormat:@"%d",x]; coords.tileCol = [NSString stringWithFormat:@"%d",y]; 有更好的方法吗?我基本上是在解析一个字符串并向数组中添加内容。我该怎么清理 更新:CoordinaryArray保留在.h中,并在dealloc中发布。我早就应该提到这一点。 完整代码: -(void)load
coords.tileRow = [NSString stringWithFormat:@"%d",x];
coords.tileCol = [NSString stringWithFormat:@"%d",y];
有更好的方法吗?我基本上是在解析一个字符串并向数组中添加内容。我该怎么清理
更新:CoordinaryArray保留在.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];
}
-(无效)载荷坐标
{
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
//获取一次坐标列表
Coordinatorray=[[NSMutableArray alloc]init];
MyAppDelegate*应用程序=[[UIApplication sharedApplication]委托];
MyData*MyData=app.data;
NSArray*myArray=[myData位置];
对于(整数i=0;i<[myArray count];i++)
{
LocationModel*loc=[myArray objectAtIndex:i];
NSArray*tmpCoords=[loc.mapCoords组件由字符串分隔:@“,”];
如果([tmpCoords计数]<2)继续;
Coordinardemodel*coords=[[Coordinardemodel alloc]init];
coords.row=[tmpCoords objectAtIndex:0];
coords.col=[tmpCoords objectAtIndex:1];
NSString*xString=[tmpCoords objectAtIndex:0];
int x=[xString floatValue]/DEFAULT\u TILE\u SIZE;
NSString*yString=[tmpCoords objectAtIndex:1];
int y=[yString floatValue]/默认值\u TILE\u大小;
coords.tileRow=[NSString stringWithFormat:@“%d”,x];
coords.tileCol=[NSString stringWithFormat:@“%d”,y];
[CoordinarRay添加对象:coords];
[协调发布];
}
[池释放];
}
坐标对象上的属性很可能声明为保留
或复制
,而您忘记了在坐标的解除锁定
方法中释放它们
泄漏仪器显示泄漏对象的创建位置,而不是丢失位置。Dave,评论不错。但是,CoordinarRay保留在.h中,并在dealloc中发布。Instruments确实可以追溯到这两个对象(由字符串分隔的组件,stringWithFormat)。我承认,我也没有看到任何明显的东西。Jordan只是出于好奇,LocationModel和CoordinationModel CoreData是自动生成的类吗?不是,LocationModel和CoordinationModel是我创建的类,它们继承自NSObject。它们不是CoreData对象。
-(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];
}