开发者

Memory Leak according to Instruments

Been running instruments on my app. Its says i am leaking 864bytes & 624bytes from 2 NSCFString and the library responsible is Foundation.

So that leads me to believe thats its not a leak caused by me? O开发者_JAVA百科r is it?

Here is the offending method according to instruments. It seems to be a

substringWithRange

that is leaking.

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

Thanks for your advice. -Code


As Gary's comment suggests this is very difficult to diagnose based on your question.

It's almost certainly a leak caused by you however, I'm afraid.

If you go to the View menu you can open the Extended Detail. This should allow you to view a stack trace of exactly where the leak occurred. This should help diagnose the problem.


When to release deckArray? If deckArray is a class member variable and not nil, should it be released before allocate and initialize memory space?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜