定位内存泄漏/过度释放崩溃的根源
谁能帮我弄清楚我应该在哪里发布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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您应该将
[super dealloc]
调用移至dealloc
方法的末尾。首先处理您的自定义变量,然后您要做的最后一件事是将dealloc推送到超类以处理其余的清理工作。现在,我担心其中的
[sortedArrayrelease]
和[sortedArrayretain]
。为了节省在sortedArray
ivar 上实现retain
/release
的语义,您应该做的就是声明的属性sortedArray
像这样:现在您可以轻松设置
sortedArray
,而不必担心保留或释放:您可以删除
release
和retain
调用现在,因为这是自动处理的。确保在dealloc
方法中添加一行来清理变量......这将为您调用数组上的
release
。编辑:这也适用于exerciseArray,这是您的实际问题。无论何时涉及 Cocoa 类,您都可以使用
@property (retain)
和@synthesize
来简化内存管理。对于NSInteger
和其他基元类型,或者当您不想持有对该对象的拥有引用时,请改用@property (assign)
。First things first: You should move the
[super dealloc]
call to the end of yourdealloc
method. Handle your custom variables first, and then the last thing you do is push thedealloc
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 implementingretain
/release
on yoursortedArray
ivar, is declaring a property forsortedArray
like so:Now you can set
sortedArray
easily without worrying about retaining or releasing:You can remove the
release
andretain
calls now, as this is handled automatically. Make sure to add a line to yourdealloc
method to clean up the variable, too....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
. ForNSInteger
and other primitive types, or when you don't want to hold an owning reference to the object, use@property (assign)
instead.