0
- (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. Can someone give me a hand?

cool_jb
  • 213
  • 2
  • 9
霍John
  • 21
  • 2
  • Swizzling a Foundation method that is frequently? That code, right there, would be enough for me to **suspect that the entire codebase is complete and utter trash that really really needs to be rewritten**. – bbum Nov 13 '18 at 20:01
  • I met the same problem, if I swizzle the objectAtIndex, the memory will increase... Really wired.. – Kirin_CN Jun 29 '23 at 03:57

2 Answers2

1

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.

The reason to create local pools is given in the NSAutoReleasePool docs (emphasis added):

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. 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; 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; or

  • Wishes to be able to call objectAtIndex and ensure there is no impact on the memory used for live objects (e.g. maybe they were measuring memory use by something else); or

  • Who knows, accept the original author (hopefully!)

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86
0

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.

Move the whole code to /dev/null.

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
  • A little harsh maybe. If you index a dictionary with a non-existent key you get `nil` back, if you index an array with a non-existent index you get an error – which isn't exactly consistent. The code as it stands fixes that inconsistency, so maybe the author just favours consistency more than you do. (That doesn't explain the autorelease pool, but that is a different issue.) – CRD Nov 13 '18 at 21:10
  • Is this about consistency. You can expect consistency if you have similar cases two times. An index is a closed range. An array is for indexed access. If you choose to use an array, you choose to take care of the index. A dictionary has no closed range of values. That's for. If you choose a dictionary, you do not choose a closed range and have to deal with it on access level. – Amin Negm-Awad Nov 14 '18 at 04:32
  • I did not say, that the key-range is fixed for arrays. I said, that it is closed, therefore a simple number describes all valid keys. This is not true for dictionaries. *This* is the difference I talked about. – Amin Negm-Awad Nov 14 '18 at 08:36
  • No, the words finite and infinite does not appear in my comments. – Amin Negm-Awad Nov 14 '18 at 11:10
  • 1
    Cleaned up the uninteresting and hence pointless thread. Consistency can clearly be in the mind of the beholder, in this case how they view the domains of two forms of discrete functions. I'll still give the original programmer the benefit of the doubt. – CRD Nov 15 '18 at 04:15