I have two questions, but let’s start with the more clearly confusing due to simplicity question first.
-(void)OnNewFrameData:(NSData *)FrameData
{
UIImage * I = [UIImage imageWithData:FrameData];
[I release];
}
If I do not release I, I experience a memory leak. Yet, I never retained I. Can somone explain this? (FrameData is managed by the caller entirely.)
Then I have another question about retained properties. Take the same code above, assign I to a retained property J, then assign nil to the same retained property, there should not be any leak, but I am leaking memory again. And I can release the retained property as well, but I still get a leak.
@property (retain) UIImage * J;
...
-(void)OnNewFrameData:(NSData *)FrameData
{
UIImage * I = [UIImage imageWithData:FrameData];
self.J = I;
// I can also add here [self.J release]; and it still leaks...
self.J = nil;
[I release];
}
Insight into these two confusing apparent contradictions from what I have learned from both the documentation and from threads here at stackoverflow would be MUCH appreciated.
So I tried an experiment with the following code…
In the code above, Img’s retain count comes back 1. If I remove the auto release pool, it is 2. This is absolutely clear evidence that imageWithData: returns an autoreleased object, and that is exactly what we all expect.
So my only conclusion is that the pool wasn’t releasing Img soon enough and they were pooling with each successive image coming over the wire. Eventually I ran out of memory.
If no one has any other ideas on the first part of the original problem I would have to go with this as the answer.
I still have no clue regarding the second issue.
UPDATE:
What had confused me all this time is how do objects on the main thread flagged for auto release get released in a timely manner? I always thought that the auto release was like a garbage collector that operated whenever the memory pool was running out of memory. But looking back at the literature, it is clear that GB is NOT what the auto release pool is about, since it is available in a non GB environment. The auto release pool doesn’t release any objects until the pool is destroyed by the drain message.
So what about main thread objects? Why do they not accumulate until the program ends? Because (I speculate here, but it is the most logical explanation) every time the framework calls into one of our class objects (UPDATE: I am talking about the framework calling our code whenever it dispatches an event), it first sets up a new auto release pool. Thus all objects in any method call to one of our class objects made by the framework is surrounded by a fresh pool and all objects flagged for auto release are released when the framework receives control back from our code.
If anyone has any information to shed more light onto this I would very much like to hear from you.