Memory Management: Revamped Engine Example
0 answers - 1683 bytes -

My goal here is to have maintenance cause the
dealloc'ing of bad parts. I'm trying to do that as
soon as they are replaced. Why? Suppose I my engine
grows in size and has millions of parts. If the parts
get put in an autorelease pool that doesn't remove
them in time, I'll have a bunch of parts that aren't
in use taking up memory. So, when they are replaced in
-maintenance, I'm trying to get them dealloc'd
immediately. I just want to make sure that is done.
Well, in that case I think you're on the right track now. At init, put the
good parts in the array, then release them so that the array is holding the
only references, and don't use autorelease. In maintenance, when they're
removed from the array they'll be released, and since they haven't been
retained elsewhere they'll be dealloc'd. (Assuming they haven't been
retained elsewhere, and it sounds like in this case that would be a bug
elsewhere in your code if they were.)
An autorelease pool is intended to help with the case where you may be
creating and releasing millions of objects in a loop, but they won't help
you here because you can't effectively use one outside of the:
1) create pool
2) run loop
3) release pool
kind of scenario. There was a discussion on this recently, but that's
basically what the warning in the docs mean about creating and releasing in
the same context of all the autoreleasing that happens in system
calls it's hard to create a pool in one method and release in another
without creating problems.