Leaksツールを実行していて、辞書のmutableDeepCopyで大量のリークが検出されましたが、コードに何が間違っているかわかりません。助言がありますか?iPhoneでメモリリークを修正する方法がわからない
@interface RootViewController : UIViewController{
NSDictionary *immutableDictionary;
NSMutableDictionary *mutableDictionary;
}
ここでここで楽器に
self.mutableDictionary = [self.immutableDictionary mutableDeepCopy];
を強調表示のコードの行は、ここで辞書
@interface NSDictionary(MutableDeepCopy)
-(NSMutableDictionary *)mutableDeepCopy;
@end
の可変コピーを作成するための方法であるが、私は「、メソッドの実装です漏れが100%漏れているというコードを強調しました
- (NSMutableDictionary *) mutableDeepCopy {
NSMutableDictionary *dictionaryToReturn = [NSMutableDictionary dictionaryWithCapacity:[self count]];
NSArray *keys = [self allKeys];
for(id key in keys) {
id value = [self valueForKey:key];
id copy = nil;
if ([value respondsToSelector:@selector(mutableDeepCopy)]) {
copy = [value mutableDeepCopy];
} else if ([value respondsToSelector:@selector(mutableCopy)]) {
copy = [value mutableCopy]; //This is the Leak
}
if (copy == nil) {
copy = [value copy];
}
[dictionaryToReturn setValue:copy forKey:key];
}
return dictionaryToReturn;
}
valueForKeyではなく/ setObject:forKey:/ setValueForKey:後者はKVC用に設計されていますが、NSDictionary上ではほとんど同じことをしていますが、よりゆっくりと微妙な違いがあります。 – JeremyP
チップJeremyのおかげで – aahrens