UIBarbutton 项目分配泄漏 - iphone

发布于 2024-11-08 12:17:59 字数 431 浏览 4 评论 0原文

我正在为右键工具栏声明一个属性并像这样分配它

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 技术交流群。

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

发布评论

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

评论(2

叹沉浮 2024-11-15 12:17:59

您过早地取消了工具栏属性 - 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.

耳钉梦 2024-11-15 12:17:59

我猜你的财产是这样的?

@property (nonatomic, retain) IBOutlet UIBarButtonItem toolBar;

这将为您自动执行保留,但您正在为您的财产提供已保留的工具栏项目。

试试这个:

if(toolBar == nil)
    toolBar = [[UIBarButtonItem alloc] initWithCustomView:tools];
    self.navigationItem.rightBarButtonItem = self.toolBar;

如果你不使用 self. 它不会使用该属性,因此不会有额外的保留:)

I guess your property is something like this?

@property (nonatomic, retain) IBOutlet UIBarButtonItem toolBar;

This will perform a retain automatically for you but you are giving your property an already retained toobar item.

Try this instead :

if(toolBar == nil)
    toolBar = [[UIBarButtonItem alloc] initWithCustomView:tools];
    self.navigationItem.rightBarButtonItem = self.toolBar;

If you don't use self. it won't use the property and therefor won't have that extra retain :)

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