OGeek|极客世界-中国程序员成长平台

标题: ios - 自动释放的 NSDictionary 的内存泄漏 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 14:20
标题: ios - 自动释放的 NSDictionary 的内存泄漏

我已经分析了我的代码,分析器显示我的方法之一是在自动释放的对象上泄漏内存。以下是相关代码的片段:

-(void) fillRSSEntriesDictionaryObject: (NSMutableDictionary *) dictionaryObject  withAllRSSEntries: (NSArray *) allRSSEntries forKey: (NSString *) keyForRSSEntriesArchive {
    RSSEntry *anRSSEntry;
    NSArray *source;
    NSMutableArray *episodes;   
    NSMutableArray *sourceArray = [[[NSMutableArray alloc] initWithObjects:nil] autorelease];
    for (int i=0; i<[allRSSEntries count]; i++) {
        source = [allRSSEntries objectAtIndex:i];   // grab the next source array.
        episodes = [[[NSMutableArray alloc] initWithObjects:nil] autorelease];  // initialize the episodes array
        for (int j=0; j<[source count]; j++) {
            anRSSEntry = [source objectAtIndex:j];
            NSDictionary *episodeDictionary = [NSDictionary dictionaryWithObjectsAndKeys:anRSSEntry.blogTitle, @"Blog Title", 
                                               anRSSEntry.articleTitle, @"Article Title", nil];
            [episodes addObject:episodeDictionary]; // save the info for this episode
        }

        [sourceArray addObject:episodes];
    }

    // Finally, we need to create the key-value pair for the source array
    [dictionaryObject setObject:sourceArray forKey: keyForRSSEntriesArchive];
}

如您所见,sourceArray 和 Episodes 是唯一分配的内存,并且两者都是自动释放的。 episodes 数组被添加到 sourceArray 数组中。 sourceArray 成为传递给调用者的对象。

profiler提供的具体信息是,责任库是“基础”,责任调用者是+[NSDictionary(NSDictionary)newWithContentsOf:immutable]。当我展开它时,它最终将我的应用程序作为负责的库,并将此方法作为负责的调用者。

既然这些是自动释放的数组,为什么分析器会提示内存泄漏?



Best Answer-推荐答案


这里没有泄漏。很可能其他一些代码稍后会使用这些对象并泄漏它们。

您可以使用 Instruments 查看泄漏对象的保留/释放历史记录,这应该可以帮助您找到额外的保留或丢失的释放。

关于ios - 自动释放的 NSDictionary 的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18665610/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4