【发布时间】:2011-06-23 12:54:56
【问题描述】:
我在调用以下方法时出现内存泄漏
- (NSArray *) children
{
NSArray *children = [node objectForKey:TFHppleNodeChildrenKey];
NSMutableArray *hpple = [NSMutableArray arrayWithCapacity:[children count]];
for(NSDictionary *child in children) {
[hpple addObject:[[TFHppleElement alloc] initWithNode:child]];
[child release];
}
return hpple;
}
我在TFHppleElement 上遇到了内存泄漏,我分配了它,但我不确定在这种情况下释放它的最佳方法是什么? TFHppleElement initWithNode 看起来像这样:
- (id) initWithNode:(NSDictionary *) theNode
{
if (!(self = [super init]))
return nil;
[theNode retain];
node = theNode;
return self;
}
【问题讨论】:
-
[child release]不应该在那里。将子元素添加到数组 children 的人不应该有额外的保留,一旦你修复了 TFHppleElement 泄漏,你就会崩溃。 -
谢谢乔,你是对的,事情就是这样。我现在已经删除了。
标签: objective-c xcode memory-leaks