• 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: Inconsistent Memory Management Rules
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Inconsistent Memory Management Rules


  • Subject: Re: Inconsistent Memory Management Rules
  • From: Lloyd Dupont <email@hidden>
  • Date: Mon, 14 Apr 2003 14:05:05 +1000

first your code is full of error, no worries I will teach you !

NSString * string = [[NSString alloc] init]; // what do you want to do
with an unmutable empty string ?
string = [string stringByAppendingString:@"Lloyd Dupont]; // this one
is very bad, what about the previous value ?

instead use
string = [[string autorelease] stringByAppendingString:@"Lloyd Dupont];
or
string = [@"" stringByAppendingString:@"Lloyd Dupont];
or
NSString * str2 = [string stringByAppendingString:@"Lloyd Dupont];
[string release];
string = str2;

[string release];
VERY bad !
all your string (save the first you forgot to release) are already
auto-released ....

explanation:
only String you explicitly allocate with method like (granted it's a
bit fuzzy):
new, alloc, create should be released by you.



On Monday, April 14, 2003, at 04:54 AM, David Blanton wrote:

> I have an example that contradicts the rules I have read for Cocoa
> memory
> management.
>
> One rule as I understand it is "If you -alloc, then you -release (or
> -autorelease)"
>
> I have code that crashes when using this rule as follows:
>
> NSString * string = [[NSString alloc]init];
> string = [string stringByAppendingString:@"steve jobs"];
>
> ... More of the same
>
> [anIBOutlet setStringValue:string];
> [string release]; // always crashes
>
> Can someone explain this inconsistency?
>
> --
> David Blanton
> "Cocoa FNG"
> _______________________________________________
> cocoa-dev mailing list | email@hidden
> Help/Unsubscribe/Archives:
> http://www.lists.apple.com/mailman/listinfo/cocoa-dev
> Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Inconsistent Memory Management Rules
      • From: Denis Stanton <email@hidden>
References: 
 >Inconsistent Memory Management Rules (From: David Blanton <email@hidden>)

  • Prev by Date: Re: focus ring not going away
  • Next by Date: Re: Inconsistent Memory Management Rules
  • Previous by thread: Re: Inconsistent Memory Management Rules
  • Next by thread: Re: Inconsistent Memory Management Rules
  • Index(es):
    • Date
    • Thread