FROM : Alastair Houghton
DATE : Wed Feb 06 16:55:56 2008
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
DATE : Wed Feb 06 16:55:56 2008
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 mail archive

