NSAutorelease 内存泄漏
我在控制台中收到此错误消息:
*** _NSAutoreleaseNoPool(): Object 0x10d2e0 of class NSPathStore2 autoreleased with no pool in place - just leaking
我无法弄清楚错误是什么?
谢谢。
I am getting this error message in the console:
*** _NSAutoreleaseNoPool(): Object 0x10d2e0 of class NSPathStore2 autoreleased with no pool in place - just leaking
I can't figure out what is the error?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个典型的内存管理问题,您在没有自动释放池的情况下自动释放了一些对象。自动释放并不是魔法。有一个 NSAutoreleasePool 类型的对象,它跟踪您自动释放的所有对象,并“不时”释放它们:
每个线程都必须有自己的自动释放池。这是非常合乎逻辑的,因为线程“同时”运行,并且如果它们共享一个公共自动释放池,则它可以在您仍在使用对象时释放该对象。
现在是重点。每个应用程序的主线程中都有一个默认的自动释放池,这意味着您不必考虑所有这些,并且自动释放的对象会被很好地收集。但是,如果您创建另一个线程,通常还被迫为此线程创建一个自动释放池。否则没有人可以声明自动释放的对象,它们就会泄漏。这正是您收到警告的原因。
没有自动释放池的泄漏线程可能如下所示:
修复很简单:
现在您只需弄清楚在另一个线程中运行代码的位置即可。
This is a classic memory management issue, you are autoreleasing some objects without having an autorelease pool in place. Autoreleasing is not a magic. There is an object of type
NSAutoreleasePool
that keeps track of all objects you autorelease and ‘from time to time’ releases them:Each thread has to have its own autorelease pool. That’s quite logical, because threads run ‘at the same time’ and if they shared a common autoreleased pool, it could release an object while you are still working with it.
Now the point. There is a default autorelease pool in the main thread of every application, which means you don’t have to think about all of this and autoreleased objects are collected just fine. But if you create another thread, you are usually forced to also create an autorelease pool for this thread. Otherwise there is nobody to claim the autoreleased objects and they just leak. Which is exactly why you are getting the warning.
Leaking thread without an autorelease pool can look like this:
The fix is simple:
Now you only have to figure out where you are running code in another thread.
听起来您已经在新线程上生成了一个方法(可能使用 + (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument;)
的任何方法在自己的线程上运行需要设置一个自动释放池来捕获任何自动释放的对象:
It sounds like you've spawned a method onto a new thread (possibly using
+ (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument;
)Any method that runs on its own thread will need to have an autorelease pool setup up to catch any autoreleased objects:
尝试使用 Clang 静态分析器
Try using the Clang Static Analyzer