定位内存泄漏/过度释放崩溃的根源

发布于 2024-11-08 02:46:35 字数 1203 浏览 0 评论 0原文

谁能帮我弄清楚我应该在哪里发布exerciseArray?当我在调用 sortedArray 之前释放时,我在 dealloc 中发生崩溃。该代码被调用多次。

//Set exerciseArray
review.exerciseArray = [[workout assignedExercises] allObjects];

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"viewWillAppear");
    NSString *workoutName =  [event.assignedWorkout valueForKey:@"name"];
    self.title = workoutName ;

    NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"index"
                                                                    ascending:YES] autorelease];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    [sortedArray release];
    sortedArray= [exerciseArray sortedArrayUsingDescriptors:sortDescriptors];
    NSLog(@"*****SORTEDARRAY****** %d",[sortedArray retainCount]);
    for (Exercise *ex in sortedArray){
        NSLog(@"%@ %@ %@",ex.name , ex.repCount, ex.weight);
    } 
    NSLog(@"*********************");
    [sortedArray retain];
    [self.tableView reloadData];
}

 - (void)dealloc {
    [super dealloc];

    [managedObjectContext release];
    [event release];
}

Can anyone help me figure out where I should be releasing exerciseArray? I get a crash in dealloc and when I release just before calling sortedArray. This code is called many times.

//Set exerciseArray
review.exerciseArray = [[workout assignedExercises] allObjects];

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"viewWillAppear");
    NSString *workoutName =  [event.assignedWorkout valueForKey:@"name"];
    self.title = workoutName ;

    NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"index"
                                                                    ascending:YES] autorelease];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    [sortedArray release];
    sortedArray= [exerciseArray sortedArrayUsingDescriptors:sortDescriptors];
    NSLog(@"*****SORTEDARRAY****** %d",[sortedArray retainCount]);
    for (Exercise *ex in sortedArray){
        NSLog(@"%@ %@ %@",ex.name , ex.repCount, ex.weight);
    } 
    NSLog(@"*********************");
    [sortedArray retain];
    [self.tableView reloadData];
}

 - (void)dealloc {
    [super dealloc];

    [managedObjectContext release];
    [event release];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

巡山小妖精 2024-11-15 02:46:35

首先,您应该将 [super dealloc] 调用移至 dealloc 方法的末尾。首先处理您的自定义变量,然后您要做的最后一件事是将dealloc推送到超类以处理其余的清理工作。


现在,我担心其中的 [sortedArrayrelease][sortedArrayretain] 。为了节省在 sortedArray ivar 上实现 retain/release 的语义,您应该做的就是声明 的属性sortedArray 像这样:

// this goes in your @interface
@property (nonatomic, retain) NSArray *sortedArray;

// this goes in your @implementation
@synthesize sortedArray;

现在您可以轻松设置 sortedArray ,而不必担心保留或释放:

// note the use of self, this is required
self.sortedArray = [exerciseArray sortedArrayUsingDescriptors:sortDescriptors];

您可以删除 releaseretain 调用现在,因为这是自动处理的。确保在 dealloc 方法中添加一行来清理变量......这

self.sortedArray = nil;

将为您调用数组上的 release

编辑:这也适用于exerciseArray,这是您的实际问题。无论何时涉及 Cocoa 类,您都可以使用 @property (retain)@synthesize 来简化内存管理。对于 NSInteger 和其他基元类型,或者当您不想持有对该对象的拥有引用时,请改用 @property (assign)

First things first: You should move the [super dealloc] call to the end of your dealloc method. Handle your custom variables first, and then the last thing you do is push the dealloc up to the superclass to handle the rest of the cleanup.


Now, I'm concerned about the [sortedArray release] and [sortedArray retain] in there. What you should be doing, to save you the semantics of implementing retain/release on your sortedArray ivar, is declaring a property for sortedArray like so:

// this goes in your @interface
@property (nonatomic, retain) NSArray *sortedArray;

// this goes in your @implementation
@synthesize sortedArray;

Now you can set sortedArray easily without worrying about retaining or releasing:

// note the use of self, this is required
self.sortedArray = [exerciseArray sortedArrayUsingDescriptors:sortDescriptors];

You can remove the release and retain calls now, as this is handled automatically. Make sure to add a line to your dealloc method to clean up the variable, too..

self.sortedArray = nil;

..which will call release on your array for you.

EDIT: This also applies to exerciseArray, which was your actual question. Wherever there's a Cocoa class involved, you can simplify memory management using @property (retain) and @synthesize. For NSInteger and other primitive types, or when you don't want to hold an owning reference to the object, use @property (assign) instead.

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