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: Rick Hoge <email@hidden>
- Date: Mon, 18 Feb 2008 22:32:56 -0500
Thanks for the reply -
When I tried to integrate my Bezier text container under 10.5 GC, I
quickly found out that my object pointers that I had stored in a
NSMutableData dynamic buffers were deadly. The GC simply doesn't
see them
and was ripping my objects out from under me. I changed it to use
NSMutableArrays containing object instead of structs (containing
object
references) and everything started working again. I haven't
determined how
much performance hit there was, but at least it now works.
Sounds like another example of where you really have to choose the
most orthodox path, using all of the prescribed methods to manage
objects, to avoid problems with the GC. Fancy pointer stuff that
might have worked under the old scheme seems to lead to trouble.
As I mentioned in the original post, I'm finding two things to be
helpful for learning - the first is switching on GC on old apps and
looking at how they break. The second is making a lot of tiny
dedicated test programs (or methods, or classes) to look at various
aspects of GC behaviour. I wish the docs were better - will file the
obligatory enhancement request...
I have to confess that, in my tiny simplified test cases, I can't
replicate a crash by accessing a pointer to the 'bytes' in an NSData
object:
-(IBAction)danglePointer:(id)sender {
int size = 1e6;
NSMutableData *testData = [NSMutableData
dataWithLength:sizeof(float)*size];
float *testPointer = (float*)[testData mutableBytes];
[[NSGarbageCollector defaultCollector] collectExhaustively]; //
Thought this would reclaim testData, but seems not
NSLog(@"Sleeping...");
sleep(10); // Give time for collector thread to do its thing -
probably not needed?
NSLog(@"Done.");
BOOL flag = YES;
if (flag) {
testPointer[0] = 3;
NSLog(@"testPointer = %f",testPointer[0]);
}
// The above code fragment does not seem to crash, ever. Why not?
}
However removing this kind of thing (use of a pointer variable for
future references to buffer in an NSData object) in larger apps has
made a number of crashes go away.
Cheers,
Rick
_______________________________________________
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