pListファイルから辞書オブジェクトの配列をロードし、それらを繰り返し処理してアプリの「チャプター」をインスタンス化するプロジェクトがあります。アナライザーは、メモリ リークがあることを通知しますが、辞書オブジェクトを pList から解放すると、アプリがクラッシュします。メモリ管理の基本は理解していますが、pList から値を保持するかコピーするかについて少し混乱しています。以下はコードです:
NSArray *chapterDictsArray = [[NSArray alloc] initWithContentsOfFile:path];
//Create array to hold all chapeters and levels
mChapters = [[NSMutableArray alloc] init];
//(this value is synthesized as @synthesized chapters = mChapters;
// defined in the header @property (nonatomic, readonly) NSMutableArray *chapters;
NSMutableDictionary *eachChapterDict = nil;
for (eachChapterDict in chapterDictsArray) {
//Create a chpater from each
ChapterData *aChapter = [[ChapterData alloc] initWithDictionary:eachChapterDict];
//[aChapter trace];
//Only add those that are enabled
if(aChapter.enabled || [aChapter isComingSoon] == YES)
{
[mChapters addObject:aChapter];
}
[aChapter release];
}
// [chapterDictsArray release];//<--uncommenting this causes the app to crash
//Set last level of game flag so we know its the last when completed
ChapterData *veryLastChapter = [mChapters objectAtIndex:[mChapters count]-2];
//Analyzer states potential leak here of an object stored in chapterDictsArray (has a retain count of +1) ???
Chapter クラスでプロパティを処理する方法が、私が書いた initWithDictionary メソッドのように問題であるかどうか疑問に思っています。次のようなプロパティを割り当てて保持しています。
self.enabled = [[aChapterDictionary objectForKey:@"enabled"] boolValue]; //header file @property (nonatomic, assign)BOOL enabled;
//and
self.instructions = [aChapterDictionary objectForKey:@"instructions"];//@property (nonatomic, retain)NSString* title;
代わりにこれらの値をコピーする必要があります。この混乱をクリーンアップする方法を混乱させたので、アナライザーはそれを報告しなくなりました。ところで、アプリは frin を実行し、これらのオブジェクトはアプリの存続期間中存在するため、現在問題は発生していませんが、何か間違っていること (またはいくつか間違っていること) は確かです。
誰かがアナライザーの警告を翻訳してくれませんか?