ObC : app crashes after returning NSMutableArray?
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:
- Starting the app
- Press button = load new view
- In the new viewDidLoad i call another object/function and send a NSMutableArray
- Process data and send back a NSMutableArray
- 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
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).
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.
精彩评论