Re: Memory management and returned values from methods...
Re: Memory management and returned values from methods...
- Subject: Re: Memory management and returned values from methods...
- From: David Duncan <email@hidden>
- Date: Fri, 15 Jul 2011 12:58:17 -0700
On Jul 15, 2011, at 12:38 PM, Kevin Muldoon wrote:
> while (fgets(buffer, sizeof(buffer), filePointer) != NULL) {
>
> NSString *line = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%s", buffer]];
You could use -[NSString initWithUTF8String:] instead. Additionally your -initWithString:<NSString instance> doesn't make a whole lot of sense in general – at best your making a copy, at but more typically your just making your code longer (and if a copy were what you were after, -copy is usually better form).
> MyObject *myObject = [[MyObject alloc] initWithString:line];
>
> //Do some very interesting things with the line in myObject...
>
> [myObject doAnInterestingMethod];
> NSString * iNeedThisString = [myObject makeAMemoryLeakAndDriveMeCrazy];
>
>
> [myObject release];
> [line release];
>
> }
Overall, wrapping this code in an autorelease pool is probably the best way to ensure that autoreleased objects don't pile up on you. You can retain the iNeedThisString to ensure it survives the pool if necessary.
--
David Duncan
_______________________________________________
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