【发布时间】:2013-09-06 19:54:10
【问题描述】:
我已经分析了我的代码,分析器显示我的方法之一是在自动释放的对象上泄漏内存。这是相关代码的sn-p:
-(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]。当我展开它时,它最终将我的应用程序作为负责的库,并将此方法作为负责的调用者。
既然这些是自动释放的数组,为什么分析器会抱怨内存泄漏?
【问题讨论】:
-
为什么这个标签是 C++?
-
对不起,我没有多想就接受了建议
-
它是否指向右侧标签中的任何代码行?
-
我采用该方法并将其添加到一个空白项目并使其编译。静态分析器没有发出任何警告,所以我猜这个问题来自其他地方,它导致了该方法的泄漏。
-
@JeffB6688 是的,我也在谈论仪器。选择要调查的泄漏线,在 Instruments 中打开右侧面板(称为 Extended Detail)。您将看到此泄漏的堆栈跟踪。双击所需的功能,它将显示负责的代码。
标签: ios objective-c memory-leaks