Autorelease pools in Objective-C - free main AutoreleasePool?

In my opinion, when an object is sent an autorelease message, if autocompletion pools do not exist, except for one in main.m , the object is placed in the object in main.m Assuming this is correct, I have a few questions:

1) Do all auto-implemented objects remain in this pool until the application expires?

2) If the value 1 is true, does an auto-implemented object create without a local pool of auto resources (therefore, it places this object in the main.m pool), storing this object in memory until a notification of receipt or memory is received?

3) When main.m autodetection pool main.m , unless the application receives a memory warning or the application terminates?

For example, in the delegate method cellForRowAtIndexPath, for example:

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Foobar"]; if (cell == nil) { // No cell to reuse => create a new one cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Foobar"] autorelease]; // lots of custom stuff } return cell; 

When are the cells really released? They must be auto-implemented because you cannot release them before you return them, and you cannot release them after they have gone out of scope. According to my current understanding, the cells are placed in the highest pool of autoadvertising and are freed when this pool is merged / freed. In this case, it will be the only pool of autoresists in the application; one in main .

4) The problem with this is that even when I finished working with these cells and the view controller was released, the cells remain in memory, right? If this is not the case, can someone explain how memory management works in this situation? Thanks!

Note. I looked at Apple's documentation, but mostly talks about when to use my own local autorun pools, but not so much about how they work.

+6
source share
2 answers

1) Do all auto-implemented objects remain in this pool until the application terminates?

Authorized objects, by definition, belong to their auto-settlement pool until this pool is exhausted. When you send -autorelease object, this object is added to the list of objects that the pool will release later. Autofill pools are organized on the stack, and the pool at the top of the stack is a pool to which any sent -autorelease objects are -autorelease . The pool created in main() is usually not the one at the top of the stack. For example, a run loop will create an autostart pool at the beginning of each iteration.

2) If 1 is true, does an auto-implemented object create without a local autorelease pool (therefore, placing this object in the main.m pool) to keep this object in memory until the application terminates or is a warning received?

This would be if the pool created in main() was the topmost pool, but as described above, this usually will not.

3) When does the auto-maintenance pool main.m merge, unless the application receives a memory warning or the application terminates?

There is no difference between the pool created in main() and any other autoresist pool. They merge when the pool is empty or at the end of the block if you used the @autorelease directive.

+5
source

From the documentation :

A set of applications creates an autostart pool in the main thread at the beginning of each cycle of the event cycle and depletes it at the end, thereby freeing up any objects with auto-realization generated during event processing. If you use the Application Kit, you usually do not need to create your own pools. However, if your application creates many temporary objects with auto-implementation in the event loop, it may be useful to create "local" autocomplete pools to minimize the maximum amount of memory.

Thus, by default, auto-implemented objects in the pool will survive only the duration of the current event.

+7
source

Source: https://habr.com/ru/post/904006/


All Articles