• 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: Release understanding help…
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Release understanding help…


  • Subject: Re: Release understanding help…
  • From: Thomas Davie <email@hidden>
  • Date: Tue, 7 Dec 2004 11:55:46 +0000


you are reassigning a new NSString that is autoreleased by the stringByAppendingString method and are leaking memory of the original string. You could initialize validChars like the following to fix it:


NSString * validChars = [[NSString alloc] initWithFormat : @"%@%@",@"0123456789mc/'\" ",[[NSUserDefaults standardUserDefaults] objectForKey:NSDecimalSeparator]];
//and remove the following line :
//validChars = [validChars stringByAppendingString:[[NSUserDefaults standardUserDefaults] objectForKey:NSDecimalSeparator]];

The alternative and possibly slightly neater and more cocoaish approach is to autorelease validChars so that it doesn't leak:


NSString* validChars = [[[NSString alloc] initWithString: @"0123456789mc/'\" "] autorelease];
validChars = [validChars stringByAppendingString:[[NSUserDefaults standardUserDefaults] objectForKey:NSDecimalSeparator]];


You don't need release at the bottom of the program, because both of the allocations are autoreleased.

Then of course you can just use the shorthand for the first statement:
NSString *validChars = @"0123456789mc/\" ";

And then you don't even need to give it a name:
validChars = [@"0123456789mc/\" " stringByAppendingString:[[NSUserDefaults standardUserDefaults] objectForKey:NSDecimalSeparator]];


This is of course a trade off though – bryon's approach is more efficient, this is possibly slightly neater looking and clearer to the programmer.

Bob


--
Computer Science is as much about computers as astronomy is about telescopes -- Edsgar Dijkstra


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Release understanding help…
      • From: j o a r <email@hidden>
References: 
 >Release understanding help… (From: Mark Dawson <email@hidden>)
 >Re: Release understanding help… (From: Byron Wright <email@hidden>)

  • Prev by Date: Re: How to create a button dynamically and set action in subclass
  • Next by Date: Re: Release understanding help…
  • Previous by thread: Re: Release understanding help Š
  • Next by thread: Re: Release understanding help…
  • Index(es):
    • Date
    • Thread