检测 NSView 何时被释放

发布于 2024-07-29 13:01:11 字数 354 浏览 8 评论 0原文

有什么方法可以检测 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 技术交流群。

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

发布评论

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

评论(1

絕版丫頭 2024-08-05 13:01:12

我之前已经对此进行了很好的研究,并且似乎没有任何方法可以观察对象何时被释放。 我见过一种在弱指针类中执行此操作的方法,但它涉及 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.

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