Re: -[NSString stringWithCString:encoding:] memory management question
Re: -[NSString stringWithCString:encoding:] memory management question
- Subject: Re: -[NSString stringWithCString:encoding:] memory management question
- From: Jeremy Pereira <email@hidden>
- Date: Thu, 12 Nov 2009 14:52:08 +0000
On 12 Nov 2009, at 14:23, Hank Heijink (Mailinglists) wrote:
> On Nov 12, 2009, at 5:59 AM, Jeremy Pereira wrote:
>
>>> for (NSUInteger i = 0; i < nTags; i++) {
>>> unsigned long pcLength = 0;
>>> if (getLengthOfMetaData(fileHandle, metadataTags[i], 0, &pcLength) != 0) continue;
>>
>> Religious rant first:
>>
>> The above line is an abomination in my opinion. What's wrong with
>>
>> if (getLengthOfMetaData(fileHandle, metadataTags[i], 0, &pcLength) == 0) {
>>
>> // rest of loop code
>>
>> }
>
> Matter of taste, in my opinion. There's nothing wrong with your version either.
That's why I prefixed it with "Religious Rant". I prefer my version because the control structure is made explicit i.e. it is obvious at a glance (with proper indentation) that all the code below the line with "if" on it is conditional.
>
>>> [tempDict setValue:contents forKey:key];
>>
>> I think this should be [tempDict setObject:contents forKey:key];
>
> Why is that? The only difference between setValue:forKey: and setObject:forKey: is that the former uses the latter unless value is nil, in which case the key will be removed. Come to think of it, using setObject:forKey: will give me the advantage of raising an exception if I try to add nil to the dictionary instead of failing silently - I think I'll switch. Thanks!
That shows that one should check the docs before saying something. I always thought setValue:forKey: was only for use with key-value coding but the docs for NSMutableDictionary seem to imply you can also use it in the more general way as you are doing.
>>> [tempDict release];
>>
>>
>> The other observation I would make is are you sure that getMetadata() completely fills your buffer from zero to pBuffer, because if it doesn't it could leave some garbage non ASCII bytes in it. This, in turn would cause initWithCString:encoding: to return nil when using the ASCII encoding. I would allocate the buffer like this:
>>
>> pBuffer = (unsigned char*) calloc(pcLength + 1, sizeof(unsigned char));
>>
>> which zeros the whole buffer. Thus copying any amount of non nul ASCII chars <= pcLength will automatically result in a C string.
>
> That's a very good point. I'll try that.
I think this is most likely where your issue lies, particularly as it has already been pointed out that initWithCString:encoding: gives undefined results if the C string is not ASCII encoded.
>
> Best,
> Hank
>
______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________
_______________________________________________
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