在我的应用上运行乐器。它说我正在泄漏864bytes&来自2个NSCFString的624bytes和负责的库是Foundation。
所以这让我相信这不是由我造成的泄漏?或者是吗?
这是根据仪器的违规方法。这似乎是一个
substringWithRange
正在泄漏。
-(void) loadDeckData
{
deckArray =[[NSMutableArray alloc] init];
NSString* path = [[NSBundle mainBundle] pathForResource:@"rugby" ofType:@"txt"
inDirectory:@""];
NSString* data = [NSString stringWithContentsOfFile:path encoding:
NSUTF8StringEncoding error: NULL];
NSString *newString = @"";
NSString *newline = @"\n";
NSString *comma = @",";
int commaCount = 0;
int rangeCount = 0;
NSString *nameHolder = @"";
NSString *infoHolder = @"";
NSMutableArray *statsHolder = [[NSMutableArray alloc] init];
for (int i=0; i<data.length; i++)
{
newString = [data substringWithRange:NSMakeRange(i, 1)];
if ([newString isEqualToString: comma]) //if we find a comma
{
if (commaCount == 0)// if it was the first comma we are parsing the
NAME
{
nameHolder = [data substringWithRange:NSMakeRange(i-
rangeCount, rangeCount)];
}
else if (commaCount == 1)//
{
infoHolder = [data substringWithRange:NSMakeRange(i-
rangeCount, rangeCount)];
//NSLog(infoHolder);
}
else // if we are on to 2nd,3rd,nth comma we are parsing stats
{
NSInteger theValue = [[data
substringWithRange:NSMakeRange(i-rangeCount,rangeCount)]
integerValue];
NSNumber* boxedValue = [NSNumber
numberWithInteger:theValue];
[statsHolder addObject:boxedValue];
}
rangeCount=0;
commaCount++;
}
else if ([newString isEqualToString: newline])
{
NSInteger theValue = [[data substringWithRange:NSMakeRange(i-
rangeCount,rangeCount)] integerValue];
NSNumber* boxedValue = [NSNumber numberWithInteger:theValue];
[statsHolder addObject:boxedValue];
commaCount=0;
rangeCount=0;
Card *myCard = [[Card alloc] init];
myCard.name = nameHolder;
myCard.information = infoHolder;
for (int x = 0; x < [statsHolder count]; x++)
{
[myCard.statsArray addObject:[statsHolder
objectAtIndex:x]];
}
[deckArray addObject:myCard];
[myCard autorelease];
[statsHolder removeAllObjects];
}
else
{
rangeCount++;
}
}
[statsHolder autorelease];
}
感谢您的建议。 -code
答案 0 :(得分:2)
正如Gary的评论所说,基于你的问题很难诊断出来。
这几乎可以肯定是你造成的泄漏,我害怕。
如果您转到“查看”菜单,则可以打开Extended Detail
。这应该允许您查看确切泄漏发生位置的堆栈跟踪。这应该有助于诊断问题。
答案 1 :(得分:1)
何时发布 deckArray ?如果 deckArray 是一个类成员变量而不是nil,是否应该在分配和初始化内存空间之前释放它?