内存泄漏的可能性;无法将 autorelease 与 UIViewController 一起使用
我无法使静态分析器“像”此代码,但同时我无法自动释放存储到控制器中的对象,因此它对调用者来说毫无用处。通过这两个静态方法,我尝试更轻松地在任何视图上显示活动控制器(不阻塞选项卡)。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一个规则(比如约定)——非自动释放的对象由方法 alloc&init...、new、retain、copy 返回。所有其余方法都必须返回自动释放的对象。
对于你的情况,我会重写上面的代码:
...
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:
...