Skip navigation.
 
mlRe: More GC. Now with NSData
FROM : Sherm Pendley
DATE : Mon Nov 26 18:56:55 2007

On Nov 26, 2007, at 12:44 PM, Alastair Houghton wrote:

> On 26 Nov 2007, at 17:20, Paul Sargent wrote:
>

>> I have an NSData that contains the complete set of data called 
>> fullDataDump. I walk through this, partitioning it up into sub-
>> blocks with code something like this:
>>
>>    fullData = [fullDataDump bytes];
>>
>>    while (currentAddress < finalAddress) {
>>        length = fullData[0];
>>     NSData newRecord = [NSData dataWithBytesNoCopy:(void *) (fullData 
>> + currentAddress) length:length freeWhenDone:NO]];
>>        [rawDives addObject:newRecord];
>>    }
>>
>> i.e. creating an array of NSDatas that point into the same memory 
>> space as the original.
>>
>> My question is this, I've created these NSDatas without copying 
>> the bytes, so have I put myself in a position where the garbage 
>> collector could release the memory beneath them if the original 
>> fullDataDump NSData was released?

>
> Yes.  The garbage collector doesn't support interior pointers.  You 
> need to keep the original object around by holding on to a 
> reference to it until you're done with all of your data objects.


Doesn't -dataWithBytesNoCopy:length:freeWhenDone: do the Right Thing 
(tm) with respect to marking the returned NSData instance as non-
collectible when the third param is NO?

IMHO, it should, and if it doesn't, that sounds like a bug.

> A better approach to the problem would be to use NSData's -
> subdataWithRange: method; that will avoid having to do pointer 
> arithmetic and will also avoid entirely the problem with the 
> garbage collector.


Good point. Consider the above a question of curiosity. :-)

sherm--

Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net

Related mailsAuthorDate
mlMore GC. Now with NSData Paul Sargent Nov 26, 18:20
mlRe: More GC. Now with NSData Alastair Houghton Nov 26, 18:44
mlRe: More GC. Now with NSData Sherm Pendley Nov 26, 18:56
mlRe: More GC. Now with NSData Alastair Houghton Nov 26, 19:00
mlRe: More GC. Now with NSData Paul Sargent Nov 26, 21:38
mlRe: More GC. Now with NSData Alastair Houghton Nov 26, 21:50