請幫忙!我已經閱讀了內存管理規則,但也許我在某處指向他們。儀器告訴我,我有以下代碼泄漏:
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%
鑰匙和對象數組不被alloc'ed或init'ed,所以我不認爲我需要釋放那些?
然後對象內部的數字被自動釋放,所以他們確定不是他們? 而sPlateToAdd是一個傳入此代碼所在方法的字符串,所以我不是它的所有者,所以我不需要釋放它。還是我?我不得不在某個地方做錯事。
的應用程序運行在iPad上完全沒問題,但狗在iPhone 3GS的慢,我希望修復這個內存泄漏可能會加速它的一點點...
這是創建自我的方法.aFinals,從文本輸入中傳遞一個字符串。我ommitted一些行,但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];
}
}
}
}
什麼樣的對象被泄露? – mja
也許,skeater意味着分配 –
_NSCFDictionary –