内存泄漏字段变量
我不明白为什么我不能简单地添加 [sortedArray release];在此方法的末尾。每次我这样做都会崩溃。
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSString *workoutName = [event.assignedWorkout valueForKey:@"name"];
self.title = workoutName ;
NSMutableArray *sortedArray = [[NSMutableArray alloc]init];
// Sort ExerciseArray
int y = 0;
int x= 0;
do{
if ([[[exerciseArray objectAtIndex:y] index]intValue] == x){
[sortedArray addObject:[exerciseArray objectAtIndex:y] ];
x++;
}
if (y<[exerciseArray count]-1){
y++;
}else {
y=0;
}
}
while(x <= [exerciseArray count]-1);
exerciseArray = sortedArray;
[self.tableView reloadData];
如果有人能指出我
正确的方向或向我推荐一些文档,我将非常感激。
I do not undserstand why I cannot simply add [sortedArray release]; at the end of this method. Everytime I do, it crashes.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSString *workoutName = [event.assignedWorkout valueForKey:@"name"];
self.title = workoutName ;
NSMutableArray *sortedArray = [[NSMutableArray alloc]init];
// Sort ExerciseArray
int y = 0;
int x= 0;
do{
if ([[[exerciseArray objectAtIndex:y] index]intValue] == x){
[sortedArray addObject:[exerciseArray objectAtIndex:y] ];
x++;
}
if (y<[exerciseArray count]-1){
y++;
}else {
y=0;
}
}
while(x <= [exerciseArray count]-1);
exerciseArray = sortedArray;
[self.tableView reloadData];
}
If anyone can point me in the right direction or refer me to some documentation, it would be very much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将数组分配给
exerciseArray
变量,然后释放该数组。下次您尝试访问exerciseArray
变量时,该数组将消失,因为将其分配给exerciseArray
变量不会增加其保留计数。当您第一次调用
alloc
时,数组对象的保留计数为 1。当你将它分配给exerciseArray时,它的retain值仍然是1。然后,当你释放它时,它的retain计数下降到0,你不能指望再次访问该数组。您应该要么不释放它(我的偏好),要么通过调用显式保留它:
注意这两条消息的顺序。第一行释放exerciseArray 变量指向的任何现有对象。第二行将新数组的保留计数提高到 2,然后第二行将其降低到 1,因此该数组仍然保留。如果您在将新数组分配给
exerciseArray
变量之前释放了sortedArray
,则保留计数将变为 0,并且在将其分配给新变量之前您将丢失它。You are assigning the array to the
exerciseArray
variable and then you are releasing the array. The next time you try to access theexerciseArray
variable, the array will be gone, because assigning it to theexerciseArray
variable doesn't increase its retain count.The array object's retain count is 1 when you first call
alloc
. When you assign it to exerciseArray, its retain value is still 1. Then, when you release it, its retain count drops to 0 and you can't expect to access the array again.You should either not release it (my preference), or else explicitly retain it by calling:
Note the order of those two messages. The first line releases any existing object pointed to by the
exerciseArray
variable. The second line raises the new array's retain count to 2, and the second then drops it to 1, so the array is still retained. If you releasesortedArray
before assigning the new array to theexerciseArray
variable, the retain count becomes 0 and you will lose it before you can assign it to the new variable.您可能想做:
顺便说一句,您可以使用带有sortedArrayUsingComparator的块对数组进行排序。
You probably wanted to do:
BTW, you can sort an Array using a block with sortedArrayUsingComparator.