Bug in CF/NSString's no-copy constructors (was Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful)
Bug in CF/NSString's no-copy constructors (was Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful)
- Subject: Bug in CF/NSString's no-copy constructors (was Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful)
- From: Alastair Houghton <email@hidden>
- Date: Wed, 6 Feb 2008 15:55:56 +0000
On 6 Feb 2008, at 09:39, John Engelhart wrote:
- (NSString *)someMethod
{
NSUInteger finalStringLength = 1024; // Example only
NSString *copySting = NULL;
char * __strong restrict copyBuffer = NULL;
copyBuffer = NSAllocateCollectable(finalStringLength, 0);
/* Since this is just an example, the part that fills contents of
copyBuffer with text are omitted */
copyString =
NSMakeCollectable
((id)CFStringCreateWithCStringNoCopy(kCFAllocatorDefault,
copyBuffer, kCFStringEncodingUTF8, kCFAllocatorNull));
/* kCFAllocatorNull = This allocator is useful as the
bytesDeallocator in CFData or contentsDeallocator in CFString where
the memory should not be freed. So.. Don't call free() on our
NSAllocateCollectable buffer, which is an error. */
return(copyString);
}
You see where the bug is, right?
I'll just add, publicly, that I think this probably is a bug in
CFString that John has found here. That is, I don't see why
CFString's pointer shouldn't be traced by the collector in this case
(it doesn't appear to be; certainly when I try it the backing buffer
is released). The problem also occurs with NSString's -
initWithBytesNoCopy:length:encoding:freeWhenDone: et al.
I've asked John if he's filed a bug report (I just filed one, <rdar://5727379
>, with a working code snippet).
Kind regards,
Alastair.
--
http://alastairs-place.net
_______________________________________________
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