Yes i know i should read more about memory management, did try to find and understand, but i still not really get why this bit of code crash when i release the NSMutableArray’s (see code). I do alloc/init initially.
- (void)readSelectedPlayers {
//Prepare File Manager
NSString *filePath = [self dataFilePath];
NSFileManager *fileMgr;
fileMgr = [NSFileManager defaultManager];
//
NSMutableArray *theObjects = [[NSMutableArray alloc] initWithCapacity:0];
NSMutableArray *activePlayersArray = [[NSMutableArray alloc] initWithCapacity:0];
NSMutableArray *readyPlayers = [[NSMutableArray alloc] initWithCapacity:0];
//
// Select all keys from the plist
NSMutableDictionary *playerDict = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
NSArray *allMyKeys = [playerDict allKeys];
//
for(NSString * myKey in allMyKeys) {
theObjects = [playerDict valueForKey:myKey];
if ([[theObjects objectAtIndex:1] intValue] == YES) {
[activePlayersArray addObject:myKey];
}
}
NSLog(@"activePlayersArray: %@", activePlayersArray);
//
//========CALL AccesQuestionDB MODULE TO SHUFFLE PLAYERS=========//
AccessQuestionsDB *shufflePlayersFunction = [AccessQuestionsDB new];
readyPlayers = [shufflePlayersFunction shufflePlayers: activePlayersArray];
NSLog(@"readyPlayers: %@", readyPlayers);
//
[readyPlayers release];
[theObjects release];
[activePlayersArray release];
}
The ‘//’ between the lines is just to get the code better formatted here.
You alloc memory for your readyPlayers variable, but then you point it to some other segment of memory:
readPlayer now is autoreleased. You lost the handle to your previously allocated memory.
What you need to do is principally:
and later… be sure to release that again if not nil when you’re done.
But… Don’t try doing this!!!
That’s what you have properties for. Make readyplayer an instance variable, declare a property for it and use synthesize. Now change the alloc to:
set the readplayer by:
No need to worry about releasing your array in dealloc etc. let the os do the error prone work for you.
If readyPlayers really is just a temporary variable, forget about the alloc and the release and let the autorelease handle it.
should return an autoreleased array.