[英]Memory leak using NSMutableArray in RootViewController called from App Delegate
我已經編寫了恢復我的應用程序狀態的代碼,但是NSMutableArray中存在內存泄漏。 我是Xcode的新手,因此如果我忽略了這瑣碎的事情,我深表歉意。 任何幫助表示贊賞。 q
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];
}
如果在viewDidUnload
中將其設置為nil
,那么將在dealloc
釋放什么? 你應該做
self.offendingNSMutableArray = nil;
在dealloc
,這是保留屬性的常用方法。
編輯:現在從上面的評論中看到它。 您需要在進行分配/初始化的地方進行自動釋放。 屬性設置器將保留。
好的,看來我通過添加自動發布來解決了泄漏:
- (void)restoreState {
// Gets an array stored in the user defaults plist
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
self.offendingNSMutableArray = [[[NSMutableArray alloc]
initWithArray:[userDefaults objectForKey:kArrayValue]] autorelease];
}
但是我以為我不想使用自動釋放功能,因為我在應用程序的其他位置引用了offendingNSMutableArray嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.