0

我有一个带有一些数据的 UITableView。每个单元格都有一个按钮,如果按下它,则会删除选定的单元格。之后,我必须使用 NSUserDefault 保存我的数据。这里有一些代码:

- (void)buttonPressed:(UIButton *)sender{   
    int tag=sender.tag;
    [myArray removeObjectAtIndex:tag];

    [self saveData];
    [self.myTableView reloadData];  
}

buttonPressed方法调用saveData方法:

-(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];
}

在调试模式下一切正常,直到saveData方法结束。当它结束调试返回buttonPressed方法时,它会重新加载 tableview,然后应用程序崩溃。我不知道为什么。

一些想法?谢谢。

4

4 回答 4

1

aEquipaggio在您的应用程序中来自哪里saveData?为什么要在循环的每次迭代中释放它?这没有意义,而且很可能过度释放,导致后来崩溃。

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-inited at the beginning because you've replaced it with an autoreleased instance in the loop. ([[NSString alloc]init] is pointless anyway, just use @"").

于 2013-05-24T09:12:05.303 回答
1
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...)

于 2013-05-24T09:13:41.947 回答
1

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.

于 2013-05-24T09:14:22.547 回答
0

一个疯狂的猜测,也许它与dataString. 尝试优化它。

于 2013-05-24T09:12:01.007 回答