0

MyAppDelegate 正在做一些后台工作,在此期间需要刷新几个视图,因此我保存了对创建的每个控制器的引用。

@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];
}

如果我执行 Product->Analyse,我会收到“潜在泄漏”和“不正确递减”警告。什么是正确的方法或假设我的方法没问题,我如何告诉分析工具忽略这些行?

4

1 回答 1

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];
}
于 2011-12-03T04:05:36.400 回答