私は 2 日間壁に頭をぶつけて、自分のコードの何が問題なのかを突き止めようとしています。これまでのところ、何もありません。私が発見した唯一のことは、オブジェクトがそれ自体で release を呼び出そうとしていて、まだインスタンス化されていないという事実です。(ただし、何らかの理由で100%発生するわけではありません。考えているときだけです)
問題を説明させてください(何かを見落としているかもしれません。皆さんが私の闇に光を当ててくれることを願っています)
私のモデルオブジェクト
Person {
NSString *name;
NSNumber *age;
}
@property(nonatomic, retain) NSNumber* TheAge;
@property(nonatomic, retain) NSString *TheName;
私の実装
@synthesize TheName = name;
@synthesize TheAge = age;
+(Person*)personFromDictionary:(NSDictionary*)dic {
Person* newPerson = [[[Person alloc] init]autorelease];
newPerson.theAge = [dic objectForKey:kAge];
newPerson.theName = [dic objectForKey:kName];
return newPerson;
}
-(void)dealloc {
self.TheAge = nil;
self.TheName = nil;
}
サーバーからJSON配列を読み取り、ダウンロードして辞書に解析する「コレクタースレッド」があります。ディクショナリのすべてのエントリは人物オブジェクトに対応します。このスレッドは、単に Person モデルを使用した別のクラスです。
スレッドはこのようなことをします (自動解放プールで)
NSDictionary *parsedDict = download.returnValue;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Person *tmpPerson = personFromDictionary
[entryDictionary setObject:tmpPerson forKey:kAge];
[pool release];
[self updateEntries:entryDictionary];
-(void)updateEntries:(NSMutableDictionary*)updatedDict {
NSArray *keys = [updatedDict allKeys];
for(NSString *key in allKeys){
Person *entry = [updatedDict valueForKey:key];
[entriesLock lock];
[currentPersonEntries setObject:entry forKey:key];
[entriesLock unlock];
}
}
クラッシュが発生すると(何らかの理由でランダムに発生し、次のようにスタックトレースが取得されます
人の割り当て解除
person setTheAge (バム クラッシュ)
セッターはこんな感じになると思います
-(void)setTheAge:(NSString*)theAge {
[theAge retain];
[age release]; // it doesn't exist for some reason???
age = theAge;
}
この種のものに対してどのように保護できますか?