简体   繁体   English

自动释放池在NSMutableArray的objectAtindex泛滥中的使用

[英]Usage of autorelease pool in objectAtindex swizzling for NSMutableArray

- (nullable id)myObjectAtIndex:(NSUInteger)index{
    @autoreleasepool {
        id value = nil;
        if (index < self.count)
        {
            value = [self myObjectAtIndex:index];
        }
        return value;
    }

}

I have no idea about the purpose of using autoreleasepool here. 我不知道在这里使用autoreleasepool的目的。 Can someone give me a hand? 有人可以帮我吗?

Unless I'm missing the obvious, which is always possible, we can only guess: 除非我总是想念显而易见的东西,否则我们只能猜测:

There is a stack of autorelease pools, the top of the stack being the one in use. 有一个自动释放池堆栈,堆栈的顶部是正在使用的池。 When the @autoreleasepool { ... } construct is entered a new pool is created and pushed on the stack, on exit from the construct the pool is drained and popped off the stack. 输入@autoreleasepool { ... }构造后,将创建一个新池并将其压入堆栈,退出构造时,该池将耗尽并从堆栈中弹出。

The reason to create local pools is given in the NSAutoReleasePool docs ( emphasis added ): NSAutoReleasePool文档中给出了创建本地池的原因(已添加重点 ):

The Application Kit creates an autorelease pool on the main thread at the beginning of every cycle of the event loop, and drains it at the end, thereby releasing any autoreleased objects generated while processing an event. Application Kit在事件循环的每个循环的开始在主线程上创建一个自动释放池,并在结束时将其耗尽,从而释放在处理事件时生成的任何自动释放对象。 If you use the Application Kit, you therefore typically don't have to create your own pools. 因此,如果使用应用程序套件,则通常不必创建自己的池。 If your application creates a lot of temporary autoreleased objects within the event loop, however, it may be beneficial to create “local” autorelease pools to help to minimize the peak memory footprint. 但是,如果您的应用程序在事件循环中创建了许多临时的自动释放对象,则创建“本地”自动释放池以帮助最大程度地减少峰值内存占用量可能是有益的。

So what is the purpose in the code you are looking at? 那么,您正在查看的代码的目的是什么? Some guesses: 一些猜测:

  • Either the original author knows/believes that the called methods count and objectAtIndex (post the swizzle) add a significant amount of objects to the autorelease pool and wishes to clean these up; 原始作者知道/相信被调用的方法countobjectAtIndex (在麻烦过后)会在自动释放池中添加大量对象,并希望清理这些对象。 or 要么

  • The original author was/is planning to add future code to myObjectAtIndex which will add a significant amount of objects to the autorelease pool and wishes to clean these up; 最初的作者正在/计划将未来的代码添加到myObjectAtIndex ,这将在自动释放池中添加大量的对象,并希望清理这些对象。 or 要么

  • Wishes to be able to call objectAtIndex and ensure there is no impact on the memory used for live objects (eg maybe they were measuring memory use by something else); 希望能够调用objectAtIndex并确保对用于活动对象的内存没有影响(例如,它们可能正在衡量其他对象的内存使用情况); or 要么

  • Who knows, accept the original author (hopefully!) 谁知道,接受原作者(希望!)

HTH 高温超导

There is no scientific reason. 没有科学的理由。

The whole code is an example of "this app crashes and I do not know why" panic code: 整个代码是“此应用程序崩溃且我不知道为什么”崩溃示例的示例:

Obviously the author had a problem with guaranteeing correct indexes, what would be the correct approach. 显然,作者在保证正确索引方面存在问题,那将是正确的方法。 Therefore he wrote a special method to "repair" it. 因此,他写了一种特殊的方法来“修复”它。 The naming ("my") shows, that he thought: I can do it better (instead of insuring correct indexes). 命名(“我的”)表明,他认为:我可以做得更好(而不是确保正确的索引)。

Moreover, adding an ARP to a piece of code, that obviously does not create an bigger amount of objects, is a sure sign for the fact, that he couldn't oversee his code anymore. 而且,在一段代码中添加ARP显然不会创建更多的对象,这确实证明了他不能再监视自己的代码了。

Move the whole code to /dev/null. 将整个代码移到/ dev / null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM