gpt4 book ai didi

ios - 根据仪器的iOS内存泄漏

转载 作者:行者123 更新时间:2023-12-01 19:26:06 24 4
gpt4 key购买 nike

请帮忙!我已经阅读了内存管理规则,但是也许我想不到它们指向了某个地方。仪器告诉我以下代码存在泄漏:

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数组没有被分配或初始化,所以我不认为我需要释放那些?

然后对象中的数字被自动释放,所以可以吗?
sPlateToAdd是一个字符串,该字符串将传递到此代码所在的方法中,因此我不是该字符串的所有者,因此不需要释放它。还是我

我必须在某处做错了事。

该应用程序在iPad上运行完全正常,但在iPhone 3GS上运行缓慢,我希望修复此内存泄漏可能会加快速度...

这是创建self.aFinals的方法,该方法从文本输入中传递一个字符串。我省略了一些线条,但self.aFinals不会与它们互动
-(id)initWithTerm:(NSString *)thisTerm {
...
...
self.aFinals = [[NSMutableArray alloc] init];

return self;
}

然后,我有大约5个嵌套循环,在所有循环中间调用了3次addPlateToFinals,创建了 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];
}
}
}

}

最佳答案

您应该显示整个`addPlateToFinals'方法,sPlateToAdd可能会泄漏。

基于新添加的代码,如果使用keep声明属性,则self.aFinals泄漏(我是%99)。应该:
self.aFinals = [[[NSMutableArray alloc] init] autorelease]
甚至更好:
self.aFinals = [NSMutableArray array]

关于ios - 根据仪器的iOS内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7491774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com