我在头文件中使用以下配置声明了 NSDate:
# TestController.h
@interface TestController : UITableViewController{
NSDate *selectedDate;
NSManagedObject *task;
}
@property (nonatomic, retain) NSManagedObject *managedObject;
@property (retain) NSDate *selectedDate
@end
# TestController.m
@implementation TestController
- (void)viewDidLoad {
self.selectedDate = [managedObject valueForKey:@"title"];
}
- (void)viewDidUnload {
self.task = nil;
self.selectedDate = nil;
}
- (void)dealloc {
[self.task release]
[self.selectedDate release];
[super dealloc];
}
@end
根据我的理解,您应该释放分配、复制或保留的变量。由于我在这里保留变量数据,所以我在dealloc时释放。但是,最终抛出内存释放错误:
[NSDate isNSDate]:发送到释放实例 0x6923750 的消息”
如果我没有手动释放它,它工作正常。我的问题是我应该为这种情况释放变量吗?
更新:
用僵尸模板进行了仪器测试,这是输出:
# Category Event Type RefCt Timestamp Address Size Responsible Library Responsible Caller
0 __NSDate Malloc 1 1331055104 0x895e9d0 16 CoreData -[NSSQLCore _prepareResultsFromResultSet:usingFetchPlan:withMatchingRows:]
1 __NSDate Retain 2 1333056000 0x895e9d0 0 Task -[TaskViewCell configureData:]
2 __NSDate Retain 3 3045008128 0x895e9d0 0 CoreData -[NSManagedObject(_NSInternalMethods) _newPropertiesForRetainedTypes:andCopiedTypes:preserveFaults:]
3 __NSDate Retain 4 3054921984 0x895e9d0 0 CoreData -[NSSQLRow copy]
4 __NSDate Release 3 3059244032 0x895e9d0 0 CoreData -[NSSQLOperation dealloc]
5 __NSDate Release 2 3059607040 0x895e9d0 0 CoreData -[NSKnownKeysDictionary1 dealloc]
6 __NSDate Retain 3 3715918848 0x895e9d0 0 CoreData -[NSManagedObject(_NSInternalMethods) _newPropertiesForRetainedTypes:andCopiedTypes:preserveFaults:]
7 __NSDate Retain 4 3727657984 0x895e9d0 0 CoreData -[NSSQLRow copy]
8 __NSDate Release 3 3730131200 0x895e9d0 0 CoreData -[NSSQLOperation dealloc]
9 __NSDate Release 2 3730286080 0x895e9d0 0 CoreData -[NSKnownKeysDictionary1 dealloc]
10 __NSDate Retain 3 4250617856 0x895e9d0 0 CoreData -[NSManagedObject(_NSInternalMethods) _newPropertiesForRetainedTypes:andCopiedTypes:preserveFaults:]
11 __NSDate Retain 4 4250759936 0x895e9d0 0 CoreData -[NSManagedObject setValue:forKey:]
12 __NSDate Release 3 4250761984 0x895e9d0 0 CoreData -[NSManagedObject setValue:forKey:]
13 __NSDate Retain 4 4260489984 0x895e9d0 0 CoreData -[NSSQLRow copy]
14 __NSDate Release 3 4263536896 0x895e9d0 0 CoreData -[NSSQLOperation dealloc]
15 __NSDate Release 2 4263687168 0x895e9d0 0 CoreData -[NSKnownKeysDictionary1 dealloc]
16 __NSDate Release 1 4656624128 0x895e9d0 0 Task -[TaskEditController dealloc]
17 __NSDate Retain 2 7570179840 0x895e9d0 0 CoreData -[NSManagedObject(_NSInternalMethods) _newPropertiesForRetainedTypes:andCopiedTypes:preserveFaults:]
18 __NSDate Retain 3 7570360064 0x895e9d0 0 CoreData -[NSManagedObject setValue:forKey:]
19 __NSDate Release 2 7570361088 0x895e9d0 0 CoreData -[NSManagedObject setValue:forKey:]
20 __NSDate Retain 3 7577499136 0x895e9d0 0 CoreData -[NSSQLRow copy]
21 __NSDate Release 2 7602608128 0x895e9d0 0 CoreData -[NSSQLOperation dealloc]
22 __NSDate Release 1 7602752256 0x895e9d0 0 CoreData -[NSKnownKeysDictionary1 dealloc]
23 __NSDate Release 0 7971558144 0x895e9d0 0 Task -[TaskEditController dealloc]
24 __NSDate Zombie -1 9697122048 0x895e9d0 0 Foundation -[NSDateFormatter stringForObjectValue:]
没看懂什么意思。但是,我猜 coredata 是否会在保留的情况下发布 NSDate?
最佳答案
解决过度释放对象问题的最佳方法是启动 Instruments(在 XCode 中,键入 ⌘-I 而不是 ⌘-R)并选择“Zombies”模板——它会告诉你在哪里每个对象都被保留和释放,并在您的代码向已释放的实例发送消息时告诉您。
在这种情况下,不平衡的释放可能潜伏在您代码中的其他地方,而在您的 dealloc 方法中放置释放会暴露其他地方的问题。在任何情况下,对您问题的直接回答是“是的,您应该释放您保留的变量”,并且您在此处发布的代码具有正确的保留与发布比率(即一对一),因此我会寻找您可能发布此 NSDate 的其他地方。
关于iphone - 我们应该手动释放 NSDate 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6054102/