我应该多久放置一次 NSAutoreleasePools ?

发布于 2024-09-30 17:53:00 字数 394 浏览 7 评论 0原文

嘿,我正在制作一个可可触摸静态库,我遇到了这个问题:

我正在使用 Leaks 工具在模拟器中运行我的项目,并且我发现了自动释放对象的泄漏。

我知道我一次至少有一个 NSAutoreleasePool (在我的 main() 方法中),我的问题是,我应该多久放入一次其他(我正在为 iPhone 和 iPad 开发,如果这很重要的话)

更新:我发现,由于某种原因,我的代码没有从 iOS 4 上的 UIApplicationMain() 调用中退出,我我刚刚收到 SIGKILL 信号,并且我的自动释放池没有耗尽。我该如何解决这个问题(我的意思是应用程序收到 SIGKILL

谢谢

Hey, I am making a cocoa touch static library, And I have this problem:

I am running my project in the simulator with the Leaks instrument, And I am coming up with leaks for autoreleased objects.

I know for a fact that I have at least one NSAutoreleasePool in place at a time (in my main() method), my question is, how often should I put in others (I am developing for iPhone and iPad if that matters)

UPDATE: I have figured out that, for some reason, my code isn't exiting out of the UIApplicationMain() call on iOS 4, I am just getting a SIGKILL signal, and my autorelease pool isn't draining. How can I fix that (I mean the app getting a SIGKILL)

Thanks

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

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

发布评论

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

评论(3

橘香 2024-10-07 17:53:00

当您在后台线程中运行某些内容时,需要 NSAutoreleasePool ,因此如果您的函数可以在后台运行,那么您需要在其中创建一个自动释放池:

- (void) willRunInBackground{
   NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
   ...
   [pool drain];
}

NSAutoreleasePool 有用的第二种情况是当您在循环中创建许多自动释放的对象 - 为了避免太多自动释放的对象挂在周围,您可以在循环迭代中创建和耗尽自动释放池(如 Joe 提到的)。

但是内存泄漏可能是由第一个原因引起的 - 每个线程必须有自己的 NSAutoreleasePool 来处理自动释放的对象。

NSAutoreleasePool is required when you run something in a background thread, so if your functions can be run in a background then you need to create a autorelease pool in them:

- (void) willRunInBackground{
   NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
   ...
   [pool drain];
}

The second situation where NSAutoreleasePool will be useful is when you create many autoreleased objects in a loop - to avoid to much autoreleased objects hanging around you can create and drain autorelease pool on loop iteration (as Joe mentioned).

But you memory leaks are likely caused by the 1st reason - each thread must have its own NSAutoreleasePool to handle autoreleased objects.

じ违心 2024-10-07 17:53:00

自动释放对象这一事实本身并不能防止内存泄漏。由于您没有在控制台中看到消息告诉您您的对象正在池外自动释放,因此这表明问题不在于它们没有被放入池中。

您一定没有正确管理保留计数。请记住,对 -alloc 和 -copy 的所有调用必须通过对 -release 或 -autorelease 的调用来平衡。也许您没有在类的 dealloc 方法中的某个地方释放成员变量。首先使用 Instruments 查找要分配/复制对象的位置,然后查看保留并释放它们的每个位置,以确保每个对象的保留计数平衡。

The fact that you are autoreleasing objects does not, in itself, prevent a memory leak. Since you're not seeing messages in Console telling your that your objects are being autoreleased outside a pool, it indicates that the problem isn't that they're not being put into a pool.

You must not be managing your retain count properly. Remember that all calls to -alloc and -copy must be balanced by calls to -release or -autorelease. Perhaps you aren't releasing your member variables in a class's dealloc method somewhere. Start by using Instruments to find where you are allocating / copying your objects, then look at every place you retain and release them to ensure each object's retain count is balanced.

尬尬 2024-10-07 17:53:00

从 WWDC 视频的标准实践来看,一个包含大量变量的紧密循环是放置变量的好地方。在循环之前启动它,循环中自动释放的所有内容都应该进入该池,然后将其耗尽。

From the WWDC videos standard practice holds that a tight loop with a lot of variables flying around is a good place to put one. Start it before the loop, everything in the loop that is autoreleased should go to that pool, and drain it afterward.

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