内存泄漏的可能性;无法将 autorelease 与 UIViewController 一起使用

发布于 2024-12-04 12:31:17 字数 991 浏览 0 评论 0原文

我无法使静态分析器“像”此代码,但同时我无法自动释放存储到控制器中的对象,因此它对调用者来说毫无用处。通过这两个静态方法,我尝试更轻松地在任何视图上显示活动控制器(不阻塞选项卡)。

PZActivityOverlayController *view = [PZActivityOverlayController displayOverView:self.view];
// Later on, when complete
[PZActivityOverlayController remove:view];

原始代码:

+ (PZActivityOverlayController *)displayOverView:(UIView *)aView {
  PZActivityOverlayController *controller = [[PZActivityOverlayController alloc] initWithFrame:aView.superview.bounds labelText:@"Loading"];
  [controller viewWillAppear:YES];
  [aView.superview insertSubview:controller.view aboveSubview:aView];
  return controller; // Potential leak of object stored into controller
}

+ (void)remove:(PZActivityOverlayController *)display {
  [display viewWillDisappear:YES];
  [display.view removeFromSuperview];
  [display release]; // However it won't leak because it gets released here
 }

也许我这里的责任链是错误的,但这只是为了方便。另一种方法是在各处编写这些方法主体中的内容(这对我来说太违反了 DRY)。

I can't make the static analyzer 'like' this code, but at the same time I cannot autorelease the object that gets stored into controller, so it is useless for the caller. With these 2 static methods I've tried to make it easier to display an activity controller over any view (without blocking tabs).

PZActivityOverlayController *view = [PZActivityOverlayController displayOverView:self.view];
// Later on, when complete
[PZActivityOverlayController remove:view];

Original code:

+ (PZActivityOverlayController *)displayOverView:(UIView *)aView {
  PZActivityOverlayController *controller = [[PZActivityOverlayController alloc] initWithFrame:aView.superview.bounds labelText:@"Loading"];
  [controller viewWillAppear:YES];
  [aView.superview insertSubview:controller.view aboveSubview:aView];
  return controller; // Potential leak of object stored into controller
}

+ (void)remove:(PZActivityOverlayController *)display {
  [display viewWillDisappear:YES];
  [display.view removeFromSuperview];
  [display release]; // However it won't leak because it gets released here
 }

Perhaps I have the chain of responsibility wrong here, but it's only for convenience. The alternative would be writing what's in the body of these methods everywhere (which violates DRY too much for me).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是一片海 2024-12-11 12:31:17

有一个规则(比如约定)——非自动释放的对象由方法 alloc&init...、new、retain、copy 返回。所有其余方法都必须返回自动释放的对象。

对于你的情况,我会重写上面的代码:

....
    return [controller autorelease];
}

+ (void)remove:(PZActivityOverlayController *)display {
    [display viewWillDisappear:YES];
    [display.view removeFromSuperview];
}

...

PZActivityOverlayController *view = [[PZActivityOverlayController displayOverView:self.view] retain];
// Later on, when complete
[PZActivityOverlayController remove:view];

[view release];

There's a rule (say, convention) - nonautoreleased objects are returned by methods alloc&init..., new, retain, copy. All the rest methods are HAVE TO return autoreleased object.

In your case I'd rewrite the code above:

....
    return [controller autorelease];
}

+ (void)remove:(PZActivityOverlayController *)display {
    [display viewWillDisappear:YES];
    [display.view removeFromSuperview];
}

...

PZActivityOverlayController *view = [[PZActivityOverlayController displayOverView:self.view] retain];
// Later on, when complete
[PZActivityOverlayController remove:view];

[view release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文