views:

40

answers:

2

Hi,

I am new to ObC and have a problem that i just cant fix. There may be other issues as well but the main issue is this:

  1. Starting the app
  2. Press button = load new view
  3. In the new viewDidLoad i call another object/function and send a NSMutableArray
  4. Process data and send back a NSMutableArray
  5. App crash, see comment where. Most often when i go back and back again but sometimes the first time

As i am new to this i guess i do a lot of this wrong but could someone nice take a look at the code and give me some advice. I would assume i have problem with releasing something.

- (void)viewDidLoad {
   [super viewDidLoad];

    NSLog(@" ");
    NSLog(@"viewDidLoad ");
    NSLog(@" ");
    NSLog(@">>Processing prepareGame<<");

    NSMutableArray *propArray1 = [[NSMutableArray alloc] initWithObjects:@"9999", nil]; //Init with dummy numbers

    AccessPropertiesFile *readMyProperties = [AccessPropertiesFile new]; //Init function call to read file

    NSLog(@"Prepare to call readProperties");

    propArray1 = [readMyProperties readPropertiesFile:propArray1];
    NSLog(@"Back from readProperties:error after this");
    /*
    for (NSString *element in propArray1) {
        NSLog(@"Elements in prop2Array; %@", element);
    }
    */ 
    [readMyProperties release];
    [propArray1 release];
}



-(NSMutableArray *)readPropertiesFile:(NSMutableArray *)readDataArray {

    NSLog(@"Processing readProperties");

    // For error information
    NSError *error;
    //Prepare File Manager
    NSString *filePath = [self dataFilePath];
    NSFileManager *fileMgr;
    fileMgr = [NSFileManager defaultManager];
    NSArray *propertiesArray = [NSArray alloc]; //Alloc array

    //Check from what module the call is coming from to ecide what to do
    if ([fileMgr fileExistsAtPath: filePath] == NO) {
        NSLog (@"File not found");
        //File does not exists, this is the first time the game starts
        //Set up default parameters
        NSString *fileString =@"0\n30\n30\n10\n1\n1\n1\n2\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n";
        // Write default parameters to file
        [fileString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
        propertiesArray = [fileString componentsSeparatedByString:@"\n"]; // each line, adjust character for line endings

    }
    else {      //File exists
        NSLog (@"File exists");
        NSString *fileString = [NSString stringWithContentsOfFile:filePath
                                                         encoding:NSUTF8StringEncoding error:nil]; // reads file into memory as an NSString
        propertiesArray = [fileString componentsSeparatedByString:@"\n"]; // each line, adjust character for line endings

    }

    //Clean readDataArray
    [readDataArray removeAllObjects];

    //Populate return array
    for (NSString *element in propertiesArray) {
        //NSLog(@"Elements in propertiesArray; %@", element);

        [readDataArray addObject:element];

    }
    NSLog(@"readDataArray: %@", readDataArray);


    [propertiesArray release];
    [readDataArray autorelease];

    NSLog(@"returning from readProperties");

    return readDataArray;
}

@end
+1  A: 

You are over-releasing readDataArray (known as propArray1 in the method that didn't create it). You create it and autorelease it in your second method, then you release it again at the end of your first method (where it wasn't created).

Joshua Nozzi
Thanks, i did take it away in the first method, with viewDidLoad, and it still breaks the second time I run the method. The first method activates when i go into 'newGame' view (viewDidLoad) i then process as above and when it is done i press the 'Back' button and go back. Second time i goto 'newGame' it breaks.
PeterK
There are a number of oddities in this code that indicate a not-so-good grasp of memory management. For example, your handling of readDataArray (and the readPropertiesFile method in general) is very strange. I recommend reviewing the memory management docs and rethinking your overall approach.
Joshua Nozzi
A: 

I suggest you use Analyze feature that comes with latest XCode. It is a good feature that I always use to track if I forget to release or release too much.

I also spotted that you also over-release the propertiesArray because it contains the result from [fileString componentsSeparatedByString:], which will be autorelease according to Cocoa convention.

tia
A BIG thank you it seems it was the over-release propertiesArray that caused the issue. Currently it looks my problem is solved :-)
PeterK