iPhone Dev Objective-C - 对象被保留,但我不知道在哪里
我正在创建一个 iPad 游戏,它有一个 viewController,可以从 nib 文件加载到其视图中。该视图中有一堆按钮,我通过界面生成器将它们链接到 UIButton * 变量(因此每个按钮都链接到不同的变量)。我在笔尖加载到其中一个按钮上后立即检查了保留计数(使用我的第一个按钮变量 b1),它给了我一个值 2。谁能解释为什么它是 2?笔尖加载后立即保留它的两件事是什么?
现在我更困惑了,因为在我的 dealloc 函数中,我单独释放了每个按钮变量,并在之后检查了其中一个变量的保留计数,它仍然是 2!它至少应该下降到 1,不是吗?我应该在 dealloc 函数中多次释放它吗?如果有,有多少?
谢谢
I'm creating an iPad game that has a viewController that loads in its view from a nib file. The view has a bunch of buttons in it, which I linked up to UIButton * variables via interface builder (so each button is linked to a different variable). I checked the retain count right after they nib was loaded on one of the buttons (using my first button variable, b1) and it gives me a value of 2. Can anyone explain why it's 2? What are the two things that are retaining it right after the nib loads?
And now I'm even more confused because in my dealloc function, I released each of the button variables individually, and checked the retain count for one of them after and it's STILL 2! It should at least have gone down to 1, shouldn't it have? Should I release it several times in my dealloc function? If so, how many?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要看保留计数。
严重地。
除了你之外的其他东西都保留着你的东西。这些数字会因为一些看似毫无意义的原因在你的下方移动,然后你会回到这里并发布困惑的问题。
只要确保您保持和释放平衡即可。这是你唯一的工作。
Don't look at retain count.
Seriously.
Things other than you retain your stuff. Those numbers will move around underneath you for reasons that appear to make no sense, and then you'll come back here and post bewildered questions.
Just make sure your retains and releases balance. That's your only job.
如果您的 IBOutlet 属性被保留,那么当按钮添加到超级视图时,您将有 1 个保留,另一个保留...
正如 Dan Ray 所说,您不应该真正担心保留计数...
If your IBOutlet properties are retained then you would have 1 retain there and another when the button gets added to the super view...
As Dan Ray says though, you shouldn't really worry about the retainCount...