直到昨天,我还以为我理解了属性内存管理的工作原理,但后来我用 XCode 运行了一个“分析”任务,并得到了很多“这个对象在这里不是自己的”。这是一个描述我的问题的简单示例:
MyObservingObject.h:
@interface MyObservingObject : NSObject
@property(nonatomic, retain) NSMutableDictionary *observedDictionary;
-(id)initWithDictCapacity:(int)capacity;
@end
MyObservingObject.m:
@synthesize observedDictionary;
-(id)initWithDictCapacity:(int)capacity {
self = [super init];
if (self) {
self.observedDictionary = [[[NSMutableDictionary alloc] initWithCapacity:capacity] autorelease];
}
return self;
}
- (void)dealloc {
// The following line makes the Analize action say :
// "Incorrect decrement of the reference count of an object that is not owned at this point by the caller"
[self.observedDictionary release], self.observedDictionary=nil;
[super dealloc];
}
我不明白的是为什么我要离开这个属性而不调用 release
它?My@property
设置为retain
(copy
也一样),所以当我这样做时self.myRetainProperty = X
,X 的保留计数增加了(它归自己所有),不是吗?