本文介绍了为什么ARC仍需要@autoreleasepool?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大多数情况下,对于ARC(自动引用计数),我们根本不需要考虑使用Objective-C对象进行内存管理.不允许再创建NSAutoreleasePool,但是有一个新语法:

For the most part with ARC (Automatic Reference Counting), we don't need to think about memory management at all with Objective-C objects. It is not permitted to create NSAutoreleasePools anymore, however there is a new syntax:

@autoreleasepool {
    …
}

我的问题是,当我不应该手动释放/自动释放时,为什么会需要这个?

My question is, why would I ever need this when I'm not supposed to be manually releasing/autoreleasing ?

编辑:总结一下我从所有答案和评论中得出的结论:

To sum up what I got out of all the anwers and comments succinctly:

新语法:

@autoreleasepool { … }是的新语法

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
…
[pool drain];

更重要的是:

  • ARC使用autoreleaserelease.
  • 它需要一个自动释放池.
  • ARC不会为您创建自动发布池. 但是:
    • 每个Cocoa应用程序的主线程中已经有一个自动释放池.
    • ARC uses autorelease as well as release.
    • It needs an auto release pool in place to do so.
    • ARC doesn't create the auto release pool for you. However:
      • The main thread of every Cocoa app already has an autorelease pool in it.
      1. 当您处于辅助线程中并且没有自动释放池时,必须自己制作以防止泄漏,例如myRunLoop(…) { @autoreleasepool { … } return success; }.
      2. 当您希望创建更多本地池时,如@mattjgalloway在其答案中所示.

    • 推荐答案

      ARC并没有摆脱保留,发布和自动发布的功能,它只是为您添加了所需的内容.因此,仍然有保留的调用,仍然有释放的调用,仍然有自动释放的调用,并且仍然有自动释放池.

      ARC doesn't get rid of retains, releases and autoreleases, it just adds in the required ones for you. So there are still calls to retain, there are still calls to release, there are still calls to autorelease and there are still auto release pools.

      他们使用新的Clang 3.0编译器和ARC进行的其他更改之一是,它们将NSAutoReleasePool替换为@autoreleasepool编译器指令.无论如何,NSAutoReleasePool总是有点特殊的对象",他们做到了这一点,因此使用一个对象的语法不会与对象混淆,因此它通常更简单一些.

      One of the other changes they made with the new Clang 3.0 compiler and ARC is that they replaced NSAutoReleasePool with the @autoreleasepool compiler directive. NSAutoReleasePool was always a bit of a special "object" anyway and they made it so that the syntax of using one is not confused with an object so that it's generally a bit more simple.

      因此,基本上,您需要@autoreleasepool,因为仍然需要担心自动发布池.您只需要担心添加autorelease调用.

      So basically, you need @autoreleasepool because there are still auto release pools to worry about. You just don't need to worry about adding in autorelease calls.

      使用自动释放池的示例:

      An example of using an auto release pool:

      - (void)useALoadOfNumbers {
          for (int j = 0; j < 10000; ++j) {
              @autoreleasepool {
                  for (int i = 0; i < 10000; ++i) {
                      NSNumber *number = [NSNumber numberWithInt:(i+j)];
                      NSLog(@"number = %p", number);
                  }
              }
          }
      }
      

      当然,这是一个非常人为的示例,但是,如果外部for循环中没有@autoreleasepool,那么以后将释放100000000个对象,而不是每次围绕外部for释放10000个对象-循环.

      A hugely contrived example, sure, but if you didn't have the @autoreleasepool inside the outer for-loop then you'd be releasing 100000000 objects later on rather than 10000 each time round the outer for-loop.

      更新:另请参阅此答案- https://stackoverflow.com/a/7950636/1068248 -为什么@autoreleasepool什么都不是与ARC有关.

      Update:Also see this answer - https://stackoverflow.com/a/7950636/1068248 - for why @autoreleasepool is nothing to do with ARC.

      更新:我查看了这里发生的情况的内部情况,并将其写在我的博客上.如果您在那儿看一眼,那么您将确切地知道ARC在做什么,新样式@autoreleasepool以及它如何引入作用域被编译器用来推断有关保留,发行和发行的信息.需要自动发布.

      Update:I took a look into the internals of what's going on here and wrote it up on my blog. If you take a look there then you will see exactly what ARC is doing and how the new style @autoreleasepool and how it introduces a scope is used by the compiler to infer information about what retains, releases & autoreleases are required.

      这篇关于为什么ARC仍需要@autoreleasepool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 05:27