2011-12-03 41 views
0

MyAppDelegate正在做一些背景的東西,在這段時間需要刷新幾個視圖,所以我保存了每個創建的控制器的引用。如何處理保存在AppDelegate中的控制器的保留計數?

@interface MyAppDelegate : NSObject <UIApplicationDelegate> { 
    SomethingController *currentSomethingController; 
} 
@property (nonatomic, retain) SomethingController *currentSomethingController; 

這樣做是爲了打開控制器:

- (void)openSomethingController { 
    MyAppDelegate * app = [[UIApplication sharedApplication] delegate]; 
    app.currentSomethingController = [[SomethingController alloc] init]; 
    [self presentModalViewController:app.currentSomethingController animated:NO]; 
} 

這就是所謂的控制器內將其關閉:

- (void)dismissSelf 
{ 
    MyAppDelegate * app = [[UIApplication sharedApplication] delegate]; 
    [app.currentSomethingController release]; 
    app.currentSomethingController = nil; 
[self dismissModalViewControllerAnimated:NO]; 
} 

在MyAppDelegate控制器是將消息發送到控制器:

- (void)longRunningBackgroundTask { 
    [currentSomethingController performSelectorOnMainThread:@selector(updateData) withObject:nil waitUntilDone:YES]; 
} 

如果我執行產品 - >分析,則會收到「潛在泄漏」和「不正確減量」警告。這樣做的正確方法或假設我的方法沒問題,我如何告訴分析工具忽略這些線?

回答

0

即使你的代碼看起來很好,你爲什麼這麼做?它會導致混亂到一個局外人閱讀你的代碼,你也應該沒有明確呼籲釋放一個屬性,你應該只讓內存管理髮生在房地產本身,所以改寫像

- (void)openSomethingController { 
    MyAppDelegate * app = [[UIApplication sharedApplication] delegate]; 
    SomethingController *controller=[[SomethingController alloc] init]; 
    app.currentSomethingController = controller; 
    [controller release]; 
    [self presentModalViewController:app.currentSomethingController animated:NO]; 
} 

,然後你的代碼

- (void)dismissSelf 
{ 
    MyAppDelegate * app = [[UIApplication sharedApplication] delegate]; 
    app.currentSomethingController = nil; 
    [self dismissModalViewControllerAnimated:NO]; 
} 
+0

我以爲財產只是保留,沒有釋放的對象,所以混亂不僅是外部讀者的。感謝您指出正確的方式來做到這一點! –