检测 NSView 何时被释放
有什么方法可以检测 NSView 何时被释放?
原因是,我有一些简单的委托(例如处理 -control:textView:doCommandBySelector: 的 NSTextField 委托,以允许输入 return/tab 键)。 我想将此委托对象粘贴在笔尖中,连接 NSTextField 的委托连接并使其工作。
它确实有效,但即使在它链接到的 NSTextField 被释放后,委托也永远不会被释放,因此委托对象泄漏。
我希望委托对象能够检测 NSTextField 何时被释放,但我想不出任何方法来做到这一点,这使得我必须存储来自其他控制器的委托对象的单独链接并在某个时刻手动释放它,这远不理想。 有任何想法吗?
Is there any way to detect when an NSView will be dealloc'ed?
The reason is, I have some simple delegates (such as an NSTextField delegate that handles -control:textView:doCommandBySelector: to allow the return/tab keys to be entered). I'd like to just stick this delegate object in the nib, wire up the NSTextField's delegate connection and have it work.
And it does work, but the delegate is never released even after the NSTextField it is linked to is released, so the delegate object leaks.
I'd like the delegate object to be able to detect when the NSTextField is dealloc'ed, but I can't think of any way to do this, which leaves me having to store a separate link to the delegate object from some other controller and manually release it at some point which is very much less than ideal. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我之前已经对此进行了很好的研究,并且似乎没有任何方法可以观察对象何时被释放。 我见过一种在弱指针类中执行此操作的方法,但它涉及 isa swizzling,这可能会变得令人讨厌。 这是网站:http://www.cocoadev.com/index.pl?WeakPointers
从 nib 文件创建的对象应该在 nib 的所有者被释放时也被释放,除非它们保留在其他地方。 例如,当 NSWindowController 被释放时,它将释放加载 nib 时创建的所有对象。 如果您的委托对象没有被释放,可能是因为它们被保留在其他地方,或者存在保留周期。
I've had a good look for this previously, and there doesn't appear to be any way to observe when an object is deallocated. I have seen one way to do it in a weak pointer class, but it involves isa swizzling, which can get nasty. Here is the website: http://www.cocoadev.com/index.pl?WeakPointers
Objects that are created from a nib file should be deallocated when the owner of the nib is deallocated, unless they are retained elsewhere. For example, when an NSWindowController is deallocated it will release any objects that were created when the nib was loaded. If your delegate objects aren't being deallocated, maybe it's because they are retained elsewhere, or there is a retain cycle.