寻找泄漏

时间:2009-08-18 18:59:19

标签: objective-c arrays memory-leaks nsdictionary

我还在处理我的泄漏问题而且我不知道如何解决这个问题我在每个arrayTmp addObject行上都有漏洞

NSMutableArray *arrayTmp= [[NSMutableArray alloc] init];


[arrayTmp addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"1", @"value", @"Value 1", @"name", nil]];
[arrayTmp addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"2", @"value", @"Value 2", @"name", nil]];
[arrayTmp addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"3", @"value", @"Value 3", @"name", nil]];
[arrayTmp addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"4", @"value", @"Value 4", @"name", nil]];
//add arrayTmp to an object to use later
[arrayTmp release];
arrayTmp=nil;

arrayTmp= [[NSMutableArray alloc] init];

[arrayTmp addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"1", @"value", @"other value 1", @"name", nil]];
[arrayTmp addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"2", @"value", @"other value 2", @"name", nil]];
//add arrayTmp to an object to use later
[arrayTmp release];
arrayTmp=nil;
...

这是我的Filter类的代码

@implementation Filter

@synthesize label;
@synthesize valueIndex;
@synthesize listValue;
@synthesize webServiceFieldName;

-(id) initWithLabel:(NSString *)plabel AndValue:(int)pvalueIndex AndListValue:(NSMutableArray *)plistValue AndWebServiceFieldName:(NSString *)pwebServiceFieldName {

self = [super init];

if (self != nil) {
    self.label = plabel;
    self.valueIndex = pvalueIndex;
    self.listValue = plistValue;
    self.webServiceFieldName = pwebServiceFieldName;

}
return self;
}

@end

4 个答案:

答案 0 :(得分:2)

//add arrayTmp to an object to use later

这意味着你将arrayTmp推入一个对象并保持它。剪切的代码在其他方面很好,但我敢打赌泄漏存在于该方法中。

发布你的方法的代码(btw,应该是-initWithLabel:value:listValue:webServiceFieldName:如果你遵循Obj-C的命名约定),它存储了arrayTmp和释放它的代码。

在Instruments中,您应该能够使用Object Alloc工具查看数组实例。单击任何一个应该为您提供该地址的分配/保留/释放/释放事件列表。您可以通过展开窗口右侧的“更多信息”窗格(通过底部的一个小按钮)来查看任何给定事件的堆栈跟踪。

从下面的代码中,初始化程序设置了ivars,包括传入的tmpArray:

    self.label = plabel;
    self.valueIndex = pvalueIndex;
    self.listValue = plistValue;
    self.webServiceFieldName = pwebServiceFieldName;

但是,我没有看到-dealloc方法。

假设您的属性是使用retaincopy声明的,那么您的dealloc应如下所示:

- (void) dealloc
{
    [label release];
    [listValue release];
    [webServiceFileName release];
    [super dealloc];
}

答案 1 :(得分:1)

您必须确保在释放此对象时释放合成成员变量 - 它不会自动为您完成。 E.g:

- (void)dealloc
{
    [label release];
    [listValue release];
    [webServiceFieldName release];

    [super dealloc];
}

答案 2 :(得分:0)

在上面的代码中,分配SearchCriteria和调用initSearchFilters的行会将结果的retainCount设置为1.然后将该对象添加到self.searchFilters然后通过addObject将retainCount提升为2.此时,您需要调用[searchCriteriaTmp release]将retainCount设置回1.当self.searchFilters被销毁时,retainCount将会为0,对象将被释放。如果没有对release的额外调用,retainCount将始终为1,并且对象将永远不会释放,从而导致泄漏。

答案 3 :(得分:-1)

好的,我做了这些修改,但我仍然在这些行上有十几个漏洞 这是我的代码示例

-(id)initSearchFilters{
    self = [super init];


    if ( self ) {

    //init the search filters array
    self.searchFilters = [[NSMutableArray alloc] init];
    SearchCriteria *searchCriteriaTmp;
    Filter *filterTmp1;
    Filter *filterTmp2;
    NSMutableArray *arrayTmp= [[NSMutableArray alloc] init];

    //Price Range
    arrayTmp = [[NSMutableArray alloc] init];

    [arrayTmp addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"any", @"value", @"any", @"name", nil]];
    [arrayTmp addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"100", @"value", @"$100", @"name", nil]];
    [arrayTmp addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"200", @"value", @"$200", @"name", nil]];
    [arrayTmp addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"300", @"value", @"$300", @"name", nil]];
    [arrayTmp addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"400", @"value", @"$400", @"name", nil]];
    [arrayTmp addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"500", @"value", @"$500", @"name", nil]];


    filterTmp1 = [[Filter alloc] initWithLabel:@"Minimum Price" AndValue:0 AndListValue:arrayTmp AndWebServiceFieldName:@"price_min"];
    filterTmp2 = [[Filter alloc] initWithLabel:@"Maximum Price" AndValue:0 AndListValue:arrayTmp AndWebServiceFieldName:@"price_max"];
    searchCriteriaTmp = [[SearchCriteria alloc] initWithFilter1:filterTmp1 AndFilter2:filterTmp2 AndLabelSearchCriteria:@"Price Range" AndEnabled:NO];
    [self.searchFilters addObject:searchCriteriaTmp];
    [arrayTmp release];
    arrayTmp=nil;
    [filterTmp1 release];
    filterTmp1=nil;
    [filterTmp2 release];
    filterTmp2=nil;
    [searchCriteriaTmp release];
    searchCriteriaTmp=nil;

    //Item
    arrayTmp = [[NSMutableArray alloc] init];

    [arrayTmp addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"1", @"value", @"item 1", @"name", nil]];
    [arrayTmp addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"2", @"value", @"item 2", @"name", nil]];
    [arrayTmp addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"3", @"value", @"item 3", @"name", nil]];
    [arrayTmp addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"4", @"value", @"item 4", @"name", nil]];

    filterTmp1 = [[Filter alloc] initWithLabel:@"Item" AndValue:0 AndListValue:arrayTmp AndWebServiceFieldName:@"item"];
    searchCriteriaTmp = [[SearchCriteria alloc] initWithFilter1:filterTmp1 AndFilter2:nil AndLabelSearchCriteria:@"Item" AndEnabled:NO];
    [self.searchFilters addObject:searchCriteriaTmp];
    [arrayTmp release];
    arrayTmp=nil;
    [filterTmp1 release];
    filterTmp1=nil;
    [searchCriteriaTmp release];
    searchCriteriaTmp=nil;
    }
    return self;
}

我的搜索条件类

-(id)initWithFilter1:(Filter *)pFilter1 AndFilter2:(Filter *)pFilter2 AndLabelSearchCriteria:(NSString *)pLabelSearchCriteria AndEnabled:(BOOL)pEnabled{

self = [super init];

if (self != nil) {
    self.filter1 = pFilter1;
    self.filter2 = pFilter2;
    self.labelSearchCriteria = pLabelSearchCriteria;
    self.enabled =pEnabled;
}
return self;
}

- (void)dealloc
{
[filter1 release];
[filter2 release];
[labelSearchCriteria release];
[super dealloc];
}