My table view has been acting a little odd and I discovered a big no no, I think. When my app launches it checks to see if there is a plist file on the iphone, If there is it instantiates 3 NSMutableArrays and adds the content from the plist. otherwise it creates a new plist file and instantiates the 3 NSMutableArrays.
I have the release for all 3 Arrays in the dealloc. When the user deletes a row I write the new information to the plist and then re load the new information from the plist again and re calculate the totals which are displayed in a UILabel.
After writing to the plist I made a mistake to say [itemsList removeAllObject]; This only removed the objects. I then made a call to read it in again [self readPlist]; and here lies my question. Is dealloc called before the new NSMutableArrays are instantiated to release them, or did I just overwrite my old arrays with new ones with the same names causing a memory leak?
In the table method that deletes the row from the table view I now added the releases for my 3 Arrays.
But now there are 2 sets of releases one in dealloc and another I release myself.
What I am unsure of is that if the program sees that it is about to allocate space for a new Array, with the same name, will it call dealloc first to release the old array before the new one is created? Or do I have to release it myself like the code snippet above? My only worry then is having 2 releases for the same object, in dealloc and in a method.
From what I read dealloc is only called as it exits the CurrentViewController. But I want to be sure I read that correctly and make sure I can have to releases for the same object.
Code:
- (void)readPlist{
NSLog(@"Reading files from plist");
NSString *filePath = [self dataFilePath];
if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
savedList = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
NSArray *tempArray = [[NSArray alloc] initWithArray: [savedList objectForKey:@"monthly"]];
monthlyPayments = [[NSMutableArray alloc] initWithArray: tempArray copyItems:YES];
[tempArray release];
NSLog(@"Files read monthly:%@",monthlyPayments);
NSArray *tempArrayTwo = [[NSArray alloc] initWithArray: [savedList objectForKey:@"total"]];
totalPayments = [[NSMutableArray alloc] initWithArray: tempArrayTwo copyItems:YES];
[tempArrayTwo release];
NSLog(@"Files read total:%@",totalPayments);
NSArray *tempArrayThree = [[NSArray alloc] initWithArray: [savedList objectForKey:@"item"]];
itemsList = [[NSMutableArray alloc] initWithArray: tempArrayThree copyItems:YES];
[tempArrayThree release];
NSLog(@"Files read item:%@",itemsList);
}
else{
savedList = [[NSMutableDictionary alloc] init];
[savedList writeToFile: filePath atomically:YES]; // if it can't find it, it writes a blank file.
NSLog(@"No Plist on file - created new one");
monthlyPayments = [[NSMutableArray alloc] init];
totalPayments = [[NSMutableArray alloc] init];
itemsList = [[NSMutableArray alloc] init];
}
}
I have the release for all 3 Arrays in the dealloc. When the user deletes a row I write the new information to the plist and then re load the new information from the plist again and re calculate the totals which are displayed in a UILabel.
After writing to the plist I made a mistake to say [itemsList removeAllObject]; This only removed the objects. I then made a call to read it in again [self readPlist]; and here lies my question. Is dealloc called before the new NSMutableArrays are instantiated to release them, or did I just overwrite my old arrays with new ones with the same names causing a memory leak?
In the table method that deletes the row from the table view I now added the releases for my 3 Arrays.
Code:
.....
[self writePlist];
[itemsList release];
[monthlyPayments release];
[totalPayments release];
[self readPlist];
.....
But now there are 2 sets of releases one in dealloc and another I release myself.
What I am unsure of is that if the program sees that it is about to allocate space for a new Array, with the same name, will it call dealloc first to release the old array before the new one is created? Or do I have to release it myself like the code snippet above? My only worry then is having 2 releases for the same object, in dealloc and in a method.
From what I read dealloc is only called as it exits the CurrentViewController. But I want to be sure I read that correctly and make sure I can have to releases for the same object.