After writing a few experiments I think I mostly understand memory management in Objective-C. Please tell me if these presumptions are correct (I'm also writing this as a matter of reinforcing my own mind on the matter):
For object obj:
1. [obj release] will decrement the object's retain count and if that count is set to 0 then [obj dealloc] will be called right then and there regardless of the autorelease pool.
2. [obj autorelease] will decrement the object's retain count when the autorelease pool is drained. If that count is 0 then [obj dealloc] will be called.
3. If obj is created via obj = [[Class alloc] init] or it is explicitly copied via some kind of copy method, I am responsible for releasing it myself.
4. If obj is created via a "factory method" (such as [NSNumber numberFromInt: x]) it's generally presumed that it was touched by [obj autorelease] before it was returned to me.
With that said, and presuming it is true, I did notice something when running these experiments last night. If I explicitly release an object that has, more or less, been set to be autoreleased when the pool drains, the program WILL seg fault.
Question 1: When working with some kind of third party library or something, how do I know that their factory methods have invoked autorelease so I know things will clean when the pool drains?
Question 2: What if I want a factory created object to be deallocated before the pool drains? Is there a way to REMOVE autorelease counters so that I can explicitly release the object without a seg fault?
Question 3: Does a memory leak eat up memory AFTER the program terminates? If I fail to properly release objects and the program quits is that memory effectively wasted until a reboot?
For object obj:
1. [obj release] will decrement the object's retain count and if that count is set to 0 then [obj dealloc] will be called right then and there regardless of the autorelease pool.
2. [obj autorelease] will decrement the object's retain count when the autorelease pool is drained. If that count is 0 then [obj dealloc] will be called.
3. If obj is created via obj = [[Class alloc] init] or it is explicitly copied via some kind of copy method, I am responsible for releasing it myself.
4. If obj is created via a "factory method" (such as [NSNumber numberFromInt: x]) it's generally presumed that it was touched by [obj autorelease] before it was returned to me.
With that said, and presuming it is true, I did notice something when running these experiments last night. If I explicitly release an object that has, more or less, been set to be autoreleased when the pool drains, the program WILL seg fault.
Question 1: When working with some kind of third party library or something, how do I know that their factory methods have invoked autorelease so I know things will clean when the pool drains?
Question 2: What if I want a factory created object to be deallocated before the pool drains? Is there a way to REMOVE autorelease counters so that I can explicitly release the object without a seg fault?
Question 3: Does a memory leak eat up memory AFTER the program terminates? If I fail to properly release objects and the program quits is that memory effectively wasted until a reboot?