내 응용 프로그램의 상태를 복원하는 코드를 작성했지만 NSMutableArray에 메모리 누수가 있습니다. 나는 Xcode에 익숙하지 않아서 이것이 간과 한 일인가 있다면 사과드립니다. 어떤 도움을 주셔서 감사합니다. 당신이 viewDidUnload
에서 nil
으로 설정하면 LQRootViewController에서 NSMutableArray를 사용하는 메모리 누수가 App Delegate에서 호출되었습니다.
AppDelegate.m
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[rootViewController restoreState];
}
RootViewController.h
@interface rootViewController : UIViewController {
NSMutableArray *offendingNSMutableArray;
}
@property (nonatomic, retain) NSMutableArray *offendingNSMutableArray;
RootViewController.m
@synthesize offendingNSMutableArray;
- (void)restoreState {
// Gets an array stored in the user defaults plist
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
self.offendingNSMutableArray = [[NSMutableArray alloc]
initWithArray:[userDefaults objectForKey:kArrayValue]];
}
- (void)viewDidUnload {
self.offendingNSMutableArray = nil;
}
- (void)dealloc {
[offendingNSMutableArray release];
}
우선, retainCount = 1로 alloc-init을 수행 한 다음 setOffendingNSMutableArray : -> retainCount = 2를 수행하십시오. viewDidUnload에서 nil로 설정하므로 retainCount = 1이고 배열에 대한 포인터를 잃어 버리고 있습니다. 너 누출이야. – beefon
미안 해요. 나는 init을 할당하지 않고 다음 줄의 코드를 사용하여 동일한 줄에 설정합니다 : self.offendingNSMutableArray = [[NSMutableArray alloc] initWithArray : [userDefaults objectForKey : kArrayValue] ]; –