无法找到可能的内存泄漏

时间:2012-07-03 06:17:24

标签: iphone objective-c

当我从xcode分析代码中的内存泄漏时,我收到警告。我试着用了一个小时来搞清楚但却无法理解,但我对特定的代码有一些怀疑:

[stack push:[outputString copy]];
[stack print]; //here xcode tell there is potential leak of an object that is createn on above line

我的堆栈实现是:

-(void) push:(NSString *)element{
[store addObject:element];
top++;
}

-(void) print{
NSLog(@"%@", [store objectAtIndex:top]);
}

- (void)dealloc {
[store release];
[super dealloc];
}

这是我的堆栈类的初始化:

-(id) initWithMutableArray{
self = [super init];
if(self){
    store = [[NSMutableArray alloc] init];
    top = -1;
}
return self;
}

我怀疑是代码[outputString copy]。但我将它存储在数组上,我在dealloc上发布商店数组。 格拉西亚斯。

1 个答案:

答案 0 :(得分:4)

没有release来平衡[outputString copy]所以它被泄露了。将对象添加到数组时,它会被数组保留并在数组被销毁时释放。