【发布时间】:2011-09-20 21:05:56
【问题描述】:
请帮忙!我已经阅读了内存管理规则,但也许我在某处错过了它们。 Instruments 告诉我以下代码有漏洞:
NSArray *keys = [NSArray arrayWithObjects:@"text", @"score", @"subCount", nil];
NSArray *objects
= [NSArray arrayWithObjects:sPlateToAdd, [
[[NSNumber alloc] initWithInt:1] autorelease],
[[[NSNumber alloc] initWithInt:1] autorelease],
nil];
NSMutableDictionary *dPlateToAdd
= [NSMutableDictionary dictionaryWithObjects:objects forKeys:keys]; // 93.4%
[self.aFinals addObject:dPlateToAdd]; // 6.6%
Keys 和 Objects 数组没有被分配或初始化,所以我认为我不需要释放它们?
那么 Objects 里面的数字会被自动释放,所以他们没问题,不是吗? sPlateToAdd 是一个字符串,它被传递到此代码所在的方法中,所以我不是它的所有者,所以我不需要释放它。还是我?
我一定在某个地方做错了什么。
该应用程序在 iPad 上运行完全正常,但在 iPhone 3GS 上运行缓慢,我希望修复此内存泄漏可能会加快速度...
这是创建 self.aFinals 的方法,它从文本输入中传递一个字符串。我省略了一些行,但 self.aFinals 不与它们交互
-(id)initWithTerm:(NSString *)thisTerm {
...
...
self.aFinals = [[NSMutableArray alloc] init];
return self;
}
然后我有大约 5 个嵌套循环,在所有循环的中间调用 addPlateToFinals 3 次,创建 thisPlate,它变成 sPlateToAdd
// replace 1st occurance
NSString *thisPlate = [thisBase
stringByReplacingOccurrencesOfRegex:[NSString stringWithFormat:
@"(^[^%@]+)%@(.*$)",
thisChar,
thisChar]
withString:[NSString stringWithFormat:@"$1%@$2", thisSub]
];
[self addPlateToFinals:thisPlate withSubCount:thisSubCount];
// replace 2nd occurance
thisPlate = [thisBase
stringByReplacingOccurrencesOfRegex:[NSString stringWithFormat:
@"(^[^%@]+%@.*)%@",
thisChar,
thisChar,
thisChar]
withString:[NSString stringWithFormat:@"$1", thisSub]
];
// then it does it again, with slightly different regex
这是泄漏的完整方法:
-(void)addPlateToFinals:(NSString *)sPlateToAdd withSubCount:(NSNumber *)nSubCount {
// plate must be less than 7 characters and great than 2 chars
if (
[sPlateToAdd length] <= [self.nPlateMax intValue] &&
[sPlateToAdd length] >= [self.nPlateMin intValue]
) {
NSMutableArray *aSearchFinals = [self arrayOfFinals];
// add plate if it is not already in the finals array
if(![aSearchFinals containsObject:sPlateToAdd]) {
// filter out results that cannot be converted to valid plates
NSPredicate *potential = [NSPredicate predicateWithFormat: @"SELF MATCHES '^[a-z]{0,3}[0-9]{1,3}[a-z]{0,3}$'"];
NSPredicate *impossible1 = [NSPredicate predicateWithFormat: @"SELF MATCHES '^[a-z]{2}[0-9]{2,3}[a-z]{2}$'"];
NSPredicate *impossible2 = [NSPredicate predicateWithFormat: @"SELF MATCHES '^[a-z][0-9]{3}$'"];
NSPredicate *impossible3 = [NSPredicate predicateWithFormat: @"SELF MATCHES '^[a-z]{2}[0-9]{2}$'"];
NSPredicate *impossible4 = [NSPredicate predicateWithFormat: @"SELF MATCHES '^[0-9]{2}[a-z]{2}$'"];
if(
[potential evaluateWithObject: sPlateToAdd] &&
![impossible1 evaluateWithObject: sPlateToAdd] &&
![impossible2 evaluateWithObject: sPlateToAdd] &&
![impossible3 evaluateWithObject: sPlateToAdd] &&
![impossible4 evaluateWithObject: sPlateToAdd]
){
NSArray *keys = [NSArray arrayWithObjects:@"text", @"score", @"subCount", nil];
NSArray *objects = [NSArray arrayWithObjects:
sPlateToAdd,
[[[NSNumber alloc] initWithInt:1] autorelease],
[[[NSNumber alloc] initWithInt:1] autorelease],
nil
];
NSDictionary *dPlateToAdd = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
[self.aFinals addObject:dPlateToAdd];
}
}
}
}
【问题讨论】:
-
什么样的对象在泄漏?
-
可能,skeater 意味着分配
-
aFinals 是
@property (retain) NSMutableArray *aFinals? -
您是在后台线程上执行吗?如果是这样,您有责任实例化和耗尽您自己的自动释放池。
-
因为你保留了你的财产,你需要像@mja建议的那样释放传递给self.aFinals的数组:self.aFinals = [NSMutableArray array];
标签: ios memory-management instruments