• 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: Memory management and returned values from methods...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

References: 
 >Memory management and returned values from methods... (From: Kevin Muldoon <email@hidden>)

  • Prev by Date: Memory management and returned values from methods...
  • Next by Date: Re: Memory management and returned values from methods...
  • Previous by thread: Memory management and returned values from methods...
  • Next by thread: Re: Memory management and returned values from methods...
  • Index(es):
    • Date
    • Thread