0

我有一个项目,我从 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 并且这些对象在应用程序的整个生命周期内都存在,所以目前不会引起任何问题,但我确定我做错了什么(或一些错误)。

有人可以为我翻译分析器警告吗?

4

0 回答 0