昨日まで、プロパティのメモリ管理がどのように機能するかを理解していると思っていましたが、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 の保持カウントが増加します (自己所有) ですね。