UIBarbutton 项目分配泄漏 - iphone
我正在为右键工具栏声明一个属性并像这样分配它
if(self.toolBar == nil)
self.toolBar = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = self.toolBar;
- (void)viewDidUnload {
toolBar = nil;
}
- (void)dealloc {
[toolBar release];
[super dealloc];
}
当我第二次进入此屏幕时(第二次调用 viewDidLoad),根据 Instruments,UIBarbuttonItem 正在泄漏。可能出什么问题了?
提前感谢您的所有帮助。
I am declaring a property for the rightbutton toolbar and allocating it like this
if(self.toolBar == nil)
self.toolBar = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = self.toolBar;
- (void)viewDidUnload {
toolBar = nil;
}
- (void)dealloc {
[toolBar release];
[super dealloc];
}
When I come to this screen for the second time(2nd time viewDidLoad called), the UIBarbuttonItem is leaking according to Instruments. What could be wrong?
Thanks for all your help in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您过早地取消了工具栏属性 - viewDidUnload 将在 dealloc 之前调用,因此 dealloc 将没有机会释放栏按钮,因为
toolBar
指向 nil,而不是对象 (将保持拥有状态(释放数量至少为 1),但没有引用)。另外,由于您没有使用点符号 (
self.toolBar
) 来清零该属性,因此旧对象的释放计数不会减少!因此,当您的控制器退出时,其保留计数至少为 2。我会在将对象分配给您的属性后立即释放该对象,因为 setter 方法无论如何都保留了它(如果您选择在声明中保留它)。稍后在 viewDidUnload 中,您需要做的就是
self.toolBar = nil;
才能真正摆脱它。You're niling the toolbar property prematurely - viewDidUnload will be called before dealloc, thus dealloc will have no chance to release the barbutton, because
toolBar
points to nil, not the object (which will remain owned (release count of at least 1) but without reference).Plus, since you're not using the dot notation (
self.toolBar
) to nil out the property, the old objects release count would not be decreased! So its retain count is at least 2 at the time your controller will quit.I'd release the object right after assigning it to your property, because the setter method has retained it anyway (if you choose to retain it in the declaration). Later in viewDidUnload all you need to do is
self.toolBar = nil;
to really get rid of it.我猜你的财产是这样的?
这将为您自动执行保留,但您正在为您的财产提供已保留的工具栏项目。
试试这个:
如果你不使用
self.
它不会使用该属性,因此不会有额外的保留:)I guess your property is something like this?
This will perform a retain automatically for you but you are giving your property an already retained toobar item.
Try this instead :
If you don't use
self.
it won't use the property and therefor won't have that extra retain :)