views:

77

answers:

2

Hi guys,

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? Or 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

+2  A: 

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.

Greg Sexton
Thanks Greg, the question was about if its me, or just instruments being odd! Thanks for the tip on Extended Detail.
Code
you want us to bet about it?
vikingosegundo
@Code, no problems. Double clicking in the stack trace (on methods from your code) takes you to the exact line of code. This is usually very helpful. Also try running a static analysis, Build and Analyze, this can be helpful tracking down leaks.
Greg Sexton
Awesome will see if i can track it to the exact line. :)
Code
+1  A: 

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

Toro