Re: -[NSString stringWithCString:encoding:] memory management question
Re: -[NSString stringWithCString:encoding:] memory management question
- Subject: Re: -[NSString stringWithCString:encoding:] memory management question
- From: Aki Inoue <email@hidden>
- Date: Wed, 11 Nov 2009 14:07:13 -0800
Another possibility.
Since you're asking to instantiate the string with NSASCIIStringEncoding, the method could return nil when it fails to convert the pBuffer contents (i.e. non-ASCII bytes above 0x7F).
That would lead to trigger exception raises at the line immediately following. That might've been confusing your memory management somewhere else.
Aki
On 2009/11/11, at 12:52, Greg Parker wrote:
> On Nov 11, 2009, at 11:58 AM, Hank Heijink (Mailinglists) wrote:
>> So, I'm curious about how -[NSString initWithCString:encoding:] works. According to the documentation, it returns "An NSString object initialized using the characters from nullTerminatedCString." Does that mean it doesn't copy the bytes? Then what does it do? -[NSString initWithUTF8String] explicitly states that it returns "An NSString object initialized by copying the bytes from bytes."
>
> -initWithCString:encoding: should copy the bytes. You can verify that yourself with a small test:
>
> char buf[10+1] = "0123456789";
> NSString *s = [[NSString alloc] initWithCString:buf encoding:NSASCIIStringEncoding];
> NSLog(@"before %@", s);
> memset(buf, 'x', 10);
> NSLog(@"after %@", s);
>
> If the bytes were not copied then `before` and `after` would differ.
>
> I don't see any memory errors in your code. My next suspect would be memory errors in getMetadata() or getLengthOfMetadata(). Try replacing getLengthOfMetadata(...) with pcLength=10, and/or getMetadata(...) with memset(pBuffer, 'x', pcLength). If one of those changes makes the crash go away then those two functions look much more suspicious.
>
>
> --
> Greg Parker email@hidden Runtime Wrangler
>
>
> _______________________________________________
>
> 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
_______________________________________________
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