为什么 NSOpenPanel/NSSavePanel 显示内存泄漏?

发布于 2024-11-28 19:05:41 字数 184 浏览 1 评论 0原文

不知道为什么,但是创建一个简单的 [[NSOpenPanel openPanel] runModal]; 会造成内存泄漏 - 在 Leaks Instrument 中可以看到。

看来关了

它是一个自动释放的对象,不是应该在 ARpool 耗尽后自动释放吗?

有办法解决这个问题吗?

Not sure why, but making a simple [[NSOpenPanel openPanel] runModal]; creates a memory leak - seen in Leaks Instrument.

Seems off.

It's an auto-released object, shouldn't it be automatically released after ARpool is drained?

Is there a way to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

花期渐远 2024-12-05 19:05:41

NSOpenPanel 是一个单例,这意味着每次使用它时您总是会获得该对象的相同实例。这意味着第一次调用 [NSOpenPanel openPanel] 时,会创建一个 NSOpenPanel 实例,但不会释放该实例。

这不是泄漏,而是优化。然而,有时 Leaks 工具会将这种一次性实例视为泄漏,因为这些实例(按照设计)从未被释放。

NSOpenPanel 是一个广泛使用且经过测试的类,其标准实现中不太可能存在任何泄漏。

NSOpenPanel is a singleton, which means you always get the same instance of the object every time you use it. This means that the first time you call [NSOpenPanel openPanel], an instance of NSOpenPanel is created and not released.

This is not a leak, it's an optimisation. However, sometimes the Leaks instrument picks up such once-only instantiations as leaks because the instances are (by design) never released.

NSOpenPanel is such a widely-used and tested class that any leaks in its standard implementation are unlikely to exist.

雨落□心尘 2024-12-05 19:05:41

NSOpenPanel 不是单例。可能曾经是这样,但是查看最新的 NSOpenPanel.h 文件可以清楚地表明它不是单例,或者至少 Apple 不希望您利用此实现细节。

至于泄漏,我很困惑何时应该释放我的开放面板并保留它。来自使用打开和保存面板 文件系统编程指南的部分,在 10.7 及更高版本中,您的生活变得更加轻松:

Important: In OS X 10.6 and earlier, you must retain an open panel prior to displaying it and release it when you are done with it. Because the openPanel method returns an autoreleased object, the panel is normally released shortly after it appears on screen. Retaining the panel prevents it from being deallocated and dismissed prematurely. You do not need to retain the panel if it is attached to a window and you do not need to retain the panel in OS X 10.7 and when using ARC.

一旦我停止保留它,事情就变得更容易并且变得很多 更轻松 :)

NSOpenPanel is not a singleton. It may have been at one time but looking at the latest NSOpenPanel.h file makes it explicitly clear it is not a singleton or at the very least Apple doesn't want you taking advantage of this implementation detail.

As for the leak, I was confused on when I should release my open panel and was retaining it. From the Using the Open and Save Panels section of the File System Programming Guide your life is a lot easier in 10.7 and above:

Important: In OS X 10.6 and earlier, you must retain an open panel prior to displaying it and release it when you are done with it. Because the openPanel method returns an autoreleased object, the panel is normally released shortly after it appears on screen. Retaining the panel prevents it from being deallocated and dismissed prematurely. You do not need to retain the panel if it is attached to a window and you do not need to retain the panel in OS X 10.7 and when using ARC.

Once I stopped retaining it things got easier and became a lot easier :)

り繁华旳梦境 2024-12-05 19:05:41

在使用 10.12 SDK 和 Swift 3.0.1 在 OS X 10.11.6 上构建的非沙盒应用程序中使用 NSOpenPanel 时,我在 Xcode 的内存图表工具中看到了“泄漏”报告。 “泄漏”是在 PlugInKit 类(PKHostPlugin、PKDiscoveryDriver 等)中报告的,即使唯一的代码行是 let openPanel = NSOpenPanel(),也会显示出来。

NSOpenPanel 的文档说明

在沙盒环境中,打开的面板是在单独的
由 powerbox 处理,而不是由 AppKit 本身处理。当用户选择一个
文件打开后,macOS 会将该文件添加到应用的沙箱中。

在我对应用程序进行沙箱处理后,“泄漏”并未出现在 Xcode 的内存图中,因为 NSOpenPanel 实现代码不再位于应用程序的地址空间中,因此我不再需要担心它。

I was seeing "leaks" reported in Xcode's memory graph tool when using NSOpenPanel in an unsandboxed app built on OS X 10.11.6 using the 10.12 SDK and Swift 3.0.1. The "leaks" were reported in PlugInKit classes (PKHostPlugin, PKDiscoveryDriver, etc.) and would show up even if the only line of code was let openPanel = NSOpenPanel().

NSOpenPanel's documentation states

In a sandboxed environment, Open panels are drawn in a separate
process by the powerbox, not by AppKit itself. When the user chooses a
file to open, macOS adds that file to the app’s sandbox.

After I sandboxed the application, the "leaks" did not show up in Xcode's memory graph as the NSOpenPanel implementation code was no longer in the application's address space, so I no longer had to worry about it.

岛歌少女 2024-12-05 19:05:41

仪器在检测泄漏方面并不完美——特别是对于自动释放的物体,并且容易出现误报。您可以尝试创建一个新的 NSAutoreleasePool,然后在完成 NSOpenPanel 后耗尽它以强制提前释放 - 但我怀疑您实际上没有泄漏。如果您确信代码看起来不错并且是自动发布的,那么它可能没问题。

Instruments is not perfect at detecting leaks - particularly for autoreleased objects, and has a tendency to have false positives. You might try creating a new NSAutoreleasePool, then draining it when you are finished with the NSOpenPanel to force release early - but I suspect you don't actually have a leak. If you are confident that the code looks good and it is autoreleased, then its probably fine.

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