Skip navigation.
 
mlRe: A coding pattern that does not work under Garbage Collection
FROM : Bill Bumgarner
DATE : Fri Nov 09 18:06:22 2007

On Nov 9, 2007, at 8:57 AM, Jim Correia wrote:
> (Not a GC expert...)
>
> No, I think you're still screwed. Since you can't know how NSData 
> manages its bucket of bytes, you need to make sure the NSData object 
> stays around for as long as you want to work with the bytes.
>
> The salient point was that no one kept a reference to the object 
> which owned the bytes you were pointing at, and when the object got 
> collected as a result, it's internal storage also went away.
>
> Really not so different from the autorelease case, except there you 
> were guaranteed the lifetime of the parent autorelease pool. In a GC 
> world the collector will collect unreferenced objects when it gets 
> around to it, so you better keep a strong reference to the object 
> for as long as you need it.


Jim is correct and this is an unfortunate edge case that there wasn't 
time to address in Leopard.  Fortunately, the solution is straight 
forward;  keep the NSData object around for as long as you need its 
memory to stick around.

b.bum