【发布时间】:2011-05-19 05:16:45
【问题描述】:
我在头文件中使用以下配置声明了 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 即使保留?
【问题讨论】:
标签: iphone ios objective-c