Re: NSData dataWithBytes:NoCopy:length free()'s the data (bug)
Re: NSData dataWithBytes:NoCopy:length free()'s the data (bug)
- Subject: Re: NSData dataWithBytes:NoCopy:length free()'s the data (bug)
- From: Allan Odgaard <email@hidden>
- Date: Fri, 1 Feb 2002 04:25:06 +0100
On fredag, februar 1, 2002, at 03:44 , Ondra Cada wrote:
You want the newly created data to use directly the
buffer "bytes", which was, though, never allocated. Therefore,
naturally,
when it is freed (as the data object gets deallocated) the memory
manager
complains.
I don't really see why NSData should call free() on my data. These data
can be allocated in a dozen other ways than using the same malloc()
operator which NSData's free() should be paired with, or they could just
be a subset of a malloc allocation..
If Apple really did decide that this would be a good idea, then I wish
they'd state it in their documentation. Cause it sure goes against my
intuition...
Use just dataWithBytes:length:. Or retain the data, never releasing it.
Or
malloc the buffer.
When I want to use the NoCopy then it's to cut down on the memory usage
and also improve the time complexity (copy is linear) -- which is the
reason I see for implementing such a feature, not to hand over the
responsibility of calling free(), the latter just make the code less
transparent, because malloc() and free() doesn't get paired
semantically...