Re: Accessing buffers in NSData/NSMutableData under garbage collection
Re: Accessing buffers in NSData/NSMutableData under garbage collection
- Subject: Re: Accessing buffers in NSData/NSMutableData under garbage collection
- From: Adam P Jenkins <email@hidden>
- Date: Tue, 19 Feb 2008 09:26:02 -0500
On Feb 19, 2008, at 1:24 AM, Chris Suter wrote:
On 19/02/2008, at 4:23 PM, Adam P Jenkins wrote:
Oops, I left out __strong, I meant the second statement to be:
__strong float *myPointer =
NSAllocateCollectable(size*sizeof(float), 0);
It doesn't make any difference.
Everything on the stack or in a register is implicitly strong.
Where did you find that information? I'm not saying it isn't true,
but I'd just like to know what else I should be reading. In the
definition of __strong in the GC Programming Guide, it has this to say:
<quote>
__strong is implicitly part of any declaration of an Objective-C
object reference type. You must use it explicitly if you need to use
Core Foundation types, void *, or other non-object references
</quote>
which led me to believe I needed to use __strong any time I want a non-
object pointer to be noticed by the GC.
Also, regarding the NSString UTF8String problem you mentioned: It
seems to me that the problem would be just as possible if you used an
NSMutableData object to allocate the memory, even if you made sure to
store a reference to the object in a variable. That is, doing
NSMutableData *data = [NSMutableData
dataWithLength:count*sizeof(float)];
float *ptr = [data mutableBytes];
// do stuff with ptr, never refer to data variable
could also land you in trouble. It seems to me that the compiler
might optimize away the data variable in the above code if it was
never accessed again after the line which calls mutableBytes, so you
could run into the same problem, where the NSMutableData object gets
GCed before you're done using ptr, unless you declared data to be
volatile. So the problem isn't with NSAllocateCollectable only.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden