内存泄漏字段变量

发布于 2024-11-08 17:11:21 字数 791 浏览 0 评论 0原文

我不明白为什么我不能简单地添加 [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光①夏 2024-11-15 17:11:21

您将数组分配给 exerciseArray 变量,然后释放该数组。下次您尝试访问 exerciseArray 变量时,该数组将消失,因为将其分配给 exerciseArray 变量不会增加其保留计数。

当您第一次调用 alloc 时,数组对象的保留计数为 1。当你将它分配给exerciseArray时,它的retain值仍然是1。然后,当你释放它时,它的retain计数下降到0,你不能指望再次访问该数组。

您应该要么不释放它(我的偏好),要么通过调用显式保留它:

[exerciseArray release];
exerciseArray = [sortedArray retain];
[sortedArray release];

注意这两条消息的顺序。第一行释放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 the exerciseArray variable, the array will be gone, because assigning it to the exerciseArray 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:

[exerciseArray release];
exerciseArray = [sortedArray retain];
[sortedArray release];

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 release sortedArray before assigning the new array to the exerciseArray variable, the retain count becomes 0 and you will lose it before you can assign it to the new variable.

过去的过去 2024-11-15 17:11:21

您可能想做:


[exerciseArray release];
exerciseArray = sortedArray;
[self.tableView reloadData];

顺便说一句,您可以使用带有sortedArrayUsingComparator的块对数组进行排序。

You probably wanted to do:


[exerciseArray release];
exerciseArray = sortedArray;
[self.tableView reloadData];

BTW, you can sort an Array using a block with sortedArrayUsingComparator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文