为什么 NSOpenPanel/NSSavePanel 显示内存泄漏?
不知道为什么,但是创建一个简单的 [[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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 ofNSOpenPanel
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.NSOpenPanel 不是单例。可能曾经是这样,但是查看最新的 NSOpenPanel.h 文件可以清楚地表明它不是单例,或者至少 Apple 不希望您利用此实现细节。
至于泄漏,我很困惑何时应该释放我的开放面板并保留它。来自使用打开和保存面板 文件系统编程指南的部分,在 10.7 及更高版本中,您的生活变得更加轻松:
一旦我停止保留它,事情就变得更容易并且变得很多 更轻松 :)
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:
Once I stopped retaining it things got easier and became a lot easier :)
在使用 10.12 SDK 和 Swift 3.0.1 在 OS X 10.11.6 上构建的非沙盒应用程序中使用
NSOpenPanel
时,我在 Xcode 的内存图表工具中看到了“泄漏”报告。 “泄漏”是在 PlugInKit 类(PKHostPlugin、PKDiscoveryDriver 等)中报告的,即使唯一的代码行是let openPanel = NSOpenPanel()
,也会显示出来。NSOpenPanel 的文档说明
在我对应用程序进行沙箱处理后,“泄漏”并未出现在 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 waslet openPanel = NSOpenPanel()
.NSOpenPanel's documentation states
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.
仪器在检测泄漏方面并不完美——特别是对于自动释放的物体,并且容易出现误报。您可以尝试创建一个新的 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.