Stk Stocki
Stk Stocki

Reputation: 139

loading file content to NSArray

I'm using this code to load content to NSArray and it seem to work fine however Instrument to detect leaks point that there is a problem that I can't put my finger on:

    - (void) loadPlan: (NSString  *) fName
    {

        short j1;

        fName= [NSString stringWithFormat:@"/%@",  fName];

        [self NewCase];

        NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docDirectory = [arrayPaths objectAtIndex:0];

        NSString *filePath = [docDirectory stringByAppendingString:fName];
        BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];

        if (!fileExists) return;

        NSString *fileContents = [NSString stringWithContentsOfFile:filePath
                                                           encoding:NSUTF8StringEncoding error:nil];


        NSArray *chunks = [fileContents componentsSeparatedByString: @"#"];


      for (i = 0; i <= 100; i++)
         {
            InputV[i] = [[chunks objectAtIndex: i+5] doubleValue];
         }


    ...    
      for (j1 = 0; j1 <= 10; j1++)
        {

            GroupMode[j1] = [[chunks objectAtIndex: 206+j1]   retain];   
        }


     ...
}

and on a init method someplace i have:

for (j1 = 0; j1 <= 10; j1++)
     {
           GroupMode[j1] = [[NSString alloc] initWithFormat:@""];
     }

Instrument points to the NSAraay *chunks line code but i'm not sure what's the issue. Do i need to release it at some point ?

I appreciate any help.

Upvotes: 0

Views: 136

Answers (3)

Analog File
Analog File

Reputation: 5316

Let me try another answer based on what you posted.

I'm assuming GroupMode is an instance variable of some class and is declared like this:

NSString* GroupMode[11];

The second loop in loadPlan should be:

  for (j1 = 0; j1 <= 10; j1++)
    {
        NSString* aChunk = [chunks objectAtIndex: 206+j1];
        if ( GroupMode[j1] != aChunk ) {
            [GroupMode[j1] release];
            GroupMode[j1] = [aChunk retain];
        }   
    }

You should do something similar every time you change an element of GroupMode and you should make sure you release all GroupMode held objects in the dealloc method of that class.

I however suggest you do not use plain arrays and instead switch to using NSArray and/or NSMutableArray.

Upvotes: 1

Analog File
Analog File

Reputation: 5316

In the comments you mention being able to call release. Therefore you are not using ARC and since I noticed you tagged with iphone you are not using GC. This leaves manual memory management.

The problem seems to be that either the chunks array or some chunks themseves are overretained (or underreleased). You are not showing all the code, so it's hard to say.

Make sure you do not retain either of them somewhere else in the code you did not show. Maybe show us the rest of the loadPlan method implementation.

edit: Now that you added more code, I can also expand this answer.

Answer this question: where is the retain call to the chunks matched with a release?

Also what is the declaration of GroupMode? It seems to be just an array of pointers. If so you probably need to release the old value before setting the new one.

Upvotes: 2

podperson
podperson

Reputation: 2383

Take a look at this answer:

"componentsSeparatedByString" Memory leak

The problem is probably that something using the results is over-retaining the stuff from chunks. Instruments is pointing at this line because it's where the memory was first allocated, but it may not be the source of your problem.

Upvotes: 0

Related Questions