• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: -[NSString stringWithCString:encoding:] memory management question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: -[NSString stringWithCString:encoding:] memory management question


  • Subject: Re: -[NSString stringWithCString:encoding:] memory management question
  • From: "Hank Heijink (Mailinglists)" <email@hidden>
  • Date: Thu, 12 Nov 2009 09:23:30 -0500

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.


[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!



			[key release];
			[contents release];
		}

		free(pBuffer);
	}

	_metaData = [[NSDictionary alloc] initWithDictionary:tempDict];
	[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.

Best,
Hank

_______________________________________________

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


  • Follow-Ups:
    • Re: -[NSString stringWithCString:encoding:] memory management question
      • From: Jeremy Pereira <email@hidden>
References: 
 >-[NSString stringWithCString:encoding:] memory management question (From: "Hank Heijink (Mailinglists)" <email@hidden>)
 >Re: -[NSString stringWithCString:encoding:] memory management question (From: Jeremy Pereira <email@hidden>)

  • Prev by Date: Re: -[NSString stringWithCString:encoding:] memory management question
  • Next by Date: Re: -[NSString stringWithCString:encoding:] memory management question
  • Previous by thread: Re: -[NSString stringWithCString:encoding:] memory management question
  • Next by thread: Re: -[NSString stringWithCString:encoding:] memory management question
  • Index(es):
    • Date
    • Thread