By my understanding, when an object is sent an autorelease
message, if no autorelease pools exist other than the one in main.m
, the object gets placed in the one in main.m
. Assuming this is correct, I have a couple of questions:
1) Do all autoreleased objects stay in that pool until the termination of the app?
2) If 1 is true, does creating an autoreleased object without a local autorelease pool (therefore placing that object in the main.m
pool) keep that object in memory until termination of the app or a memory warning is received?
3) When is the main.m
autorelease pool drained, other than when the app receives a memory warning or the application is terminated?
For example, in a cellForRowAtIndexPath delegate method such as this:
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 actually released? They have to be autoreleased, because you can't release them before you return them, and you can't release them after, because they are out of scope. By my current understanding, the cells are placed in the topmost autorelease pool, and released when that pool is drained/released. In this case, that would be the only autorelease pool in the application; the one in main
.
4) The problem with this it that even when I am done with those cells and the view controller has been released, the cells stay in memory, yes? If this is not the case, could someone provide an explanation of how the memory management in this situation really works? Thanks!
Note: I have looked at Apple's documentation, but it talks mostly about when to use your own local autorelease pools, but not much about how they actually function.