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: Chris Suter <email@hidden>
- Date: Tue, 19 Feb 2008 17:24:21 +1100
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.
The other thing to be careful of when allocating arrays like this is
that pointers to anything other than the beginning of the array aren't
considered. So, for example, you'd be on unsafe ground with something
like the following:
float *myPointer = NSAllocateCollectable(size * sizeof(float), 0);
for (n = 0; n < size; ++n, ++myPointer) {
// Do something with myPointer
}
I don't use garbage collection myself so someone with more experience
might know a way of getting interior pointers to work (I believe
that's the name for them). Even if there was a way, I'm not sure it's
a good idea…
You could still be in trouble if you decided to do something like:
float *myArray = NSAllocateCollectable(size * sizeof(float), 0);
// myArray is not referenced after the following line
for (n = 0, myPointer = myArray; n < size; ++n, ++myPointer) {
// Do something with myPointer
}
In this case, there's nothing stopping the compiler turning it into
the same as my first example. For this reason, I don't think I'd be
allocating temporary garbage collected arrays like this—you're relying
on the compiler to leave the right pointer on the stack or in a
register but with optimisations that may not be the case. You'll run
into similar problems if you try and use NSMutableData.
What is needed, I believe, is a way of getting auto-released memory.
This would solve this problem and also things like -[NSString
UTF8String] which suffer from the same problem, and are worse because
you often want to pass the result of it to library routines (which you
have no control over).
- Chris
p.s. here's the test program I just used:
#include <setjmp.h>
#include <Foundation/Foundation.h>
static __strong float *p;
static __strong const char *x;
int main ()
{
jmp_buf env;
if (!setjmp (env)) {
p = (float *)NSAllocateCollectable (100, 0) + 1;
*p = 567.89f;
x = [@"1234" UTF8String] + 1;
printf ("%f %s\n", *p, x);
longjmp (env, 1);
}
[[NSGarbageCollector defaultCollector] collectExhaustively];
printf ("%f %s\n", *p, x);
return 0;
}
dog:tmp csuter$ MallocScribble=YES ./test
test(25679) malloc: enabling scribbling to detect mods to free blocks
567.890015 234
nan UUUUUUUUUUU?
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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