1

我仍在处理我的泄漏问题,我不知道如何解决这个问题我在每个 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;
...

这是我的过滤器类的代码

@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

4 回答 4

2
//add arrayTmp to an object to use later

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

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

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

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

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

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

假设您的属性是用retainor声明的copy,您的 dealloc 应该如下所示:

- (void) dealloc
{
    [label release];
    [listValue release];
    [webServiceFileName release];
    [super dealloc];
}
于 2009-08-18T22:02:34.187 回答
1

您必须确保在释放此对象时释放合成的成员变量——它不会自动为您完成。例如:

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

    [super dealloc];
}
于 2009-08-18T23:08:54.847 回答
0

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

于 2009-08-19T21:40:02.673 回答
-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];
}
于 2009-08-19T00:03:26.703 回答