Le 20 mars 08 à 06:26, Michael Ash a écrit :

On Thu, Mar 20, 2008 at 12:56 AM, Jens Alfke <[EMAIL PROTECTED]> wrote:
It's never a good idea to
make assumptions about where a Foundation object is putting its data;
if you need to access the current bytes of an NSData, call -bytes on it.

This was a little confusing to me until I thought about it a bit, so I
thought I would emphasize this.

When you use these methods and tell it to free the memory, the object
you create takes ownership of the block of memory. This means that
it's responsible for freeing it, but more importantly it means that
it's *in control* of freeing it. In other words, conceptually speaking
it's never safe to refer back to this block of memory directly after
you create the object. Instead you should query the object to get
whatever you need from it, in this case query the -bytes method to get
the pointer back.

If I understand the API contract, the code in question is not even
safe when using NSData. It happens to work, but the contract does not
guarantee it. To be safe, the NSData should be created, then the
return value from its -bytes method should be returned. If I'm wrong
about this, I'd appreciate any corrections.

Mike

-bytes returns a (const void *) that you should not change.
If you want to create a raw buffer using NSData, use NSMutableData and -mutableBytes method.

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to