Reputation: 351
I have a UITableView with some data. Every cell has a button and if you press it you delete the cell selected. After that I have to save my data with NSUserDefault. Here some code:
- (void)buttonPressed:(UIButton *)sender{
int tag=sender.tag;
[myArray removeObjectAtIndex:tag];
[self saveData];
[self.myTableView reloadData];
}
the buttonPressed
method calls saveData
method:
-(void)saveData{
dataString=[[NSString alloc]init]; //defined in .h file
for(int i=0; i<([myArray count]); i++){
ClassObject *aObject=[[ClassObject alloc]init];
aObject=[myArray objectAtIndex:i];
dataString=[dataString stringByAppendingString:aObject.idObject];
dataString=[dataString stringByAppendingString:@"$"];
dataString=[dataString stringByAppendingString:aObject.description1];
dataString=[dataString stringByAppendingString:@"$"];
dataString=[dataString stringByAppendingString:aObject.description2];
dataString=[dataString stringByAppendingString:@"?"];
[aObject release];
}
NSUserDefaults *dataDefault=[NSUserDefaults standardUserDefaults];
[dataDefault setObject:dataString forKey:@"myDataString"];
[dataDefault synchronize];
[dataString release];
}
in debug mode everything goes fine until the saveData
method ends. When it ends the debug returns on buttonPressed
method, it reloads the tableview and then the app crashes. And I don't know why.
Some ideas? Thanks.
Upvotes: 0
Views: 345
Reputation:
ClassObject *aObject=[[ClassObject alloc]init];
aObject=[arrayEquipaggio objectAtIndex:i];
// ...
[aObject release];
Firstly, and most importantly, it's rather anObject
.
Secondly, this both leaks memory (you lose the pointer to the allocated instance when you re-assign [arrayEquipaggio objectAtIndex:i];
to the variable) and over-releases aObject
(which now, as I just explained, points to an object within the array, so you are releasing an object you don't own).
All in all, remove the line with the alloc-init and that with the release (you make the very same mistake with the dataString
variable too) and:
Read this before continuing with development! Else you will have serious trouble doing anything in Objective-C. Currently you seem to have no idea about how memory management works. (It would also be worth to learn C before trying to use Objective-C, but unfortunately that turns out to be too big an expectation...)
Upvotes: 1
Reputation: 104065
You’ve got the memory allocation wrong. This is what you’re doing:
dataString = [[NSString alloc] init];
dataString = [dataString stringByAppendingString:@"$"];
[dataString release];
First you allocate a new string that you have to release later. That’s OK. Then you store a different pointer into the dataString
variable, leaking the previous string. And then you release the autoreleased string created by -stringByAppendingString
, so that the object gets over-released soon after that.
Upvotes: 1
Reputation: 53551
Where does aEquipaggio
come from in your saveData
app? Why are you releasing it in each iteration of the loop? That doesn't make sense and it's very likely over-released, causing the crash later.
Edit: Now that you changed the code in your question, aEquipaggio
is no longer there, but the release
at the end of the loop is still incorrect, and the first line in the loop is entirely pointless (and leaks) because you never use the ClassObject
instance you allocate.
You also shouldn't be releasing dataString
at the end of the method. At that point, dataString
no longer contains the same instance that you alloc
-init
ed at the beginning because you've replaced it with an autoreleased instance in the loop. ([[NSString alloc]init]
is pointless anyway, just use @""
).
Upvotes: 1
Reputation: 2814
A wild guess, maybe it has something to do with the releasing of the dataString
. Try optimizing it.
Upvotes: 0