• 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 question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory Management question


  • Subject: Re: Memory Management question
  • From: Paul Lynch <email@hidden>
  • Date: Mon, 27 Mar 2006 23:21:55 +0100


On 27 Mar 2006, at 22:55, j o a r wrote:


On 25 mar 2006, at 14.16, Bruce Truax wrote:

There are two different ways I could write the code for stringRepresentation. I believe that they are equivalent but I want to make sure that they both work.
<snip>
IMPLEMENTATION 1
returnString = [NSString stringWithFormat:@"%@ %@, %e %e %e %e %e\n", [...]
return returnString;
<snip>
IMPLEMENTATION 2
returnString = [[NSString alloc initWithFormat:@"%@ %@, %e %e % e %e [...]
return [returnString autorelease];
<snip>
If I understand the memory management documentation correctly, both of these
methods return an autoreleased NSString. Is this correct?

True. They are equal from a memory management perspective. Slight typo in the second variant though...
;-)


That said I would go with your version #2 because it's more expressive. It clearly states that you're interested in a temporary string.
I also don't like #1 as you've separated the autorelease from the alloc+init. This opens the door for a memory leak in the future, as you can copy the alloc+init line to somewhere else in your code, forgetting to also copy over the autorelease. If you need to use alloc+init in a place like this, consider doing alloc+init +autorelease all on one line.

I assume that you transposed 2 and 1, as I came to the same reasons as you but for the other numbers. 1 calls stringWith..., 2 uses alloc/init, then return autorelease.


There's nothing wrong with serialising via strings, if you can get extra use out of the format - like debugging information. In which case, I'd go with overriding description. I know that someone a couple of weeks ago had a problem with using description in some circumstances, but this seems an example where it would suit.

Paul
_______________________________________________
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: Memory Management question
      • From: j o a r <email@hidden>
References: 
 >Memory Management question (From: Bruce Truax <email@hidden>)
 >Re: Memory Management question (From: j o a r <email@hidden>)

  • Prev by Date: Re: -[NSString localizedCompare:] - example of semantic difference with compare?
  • Next by Date: Re: thread insanity...
  • Previous by thread: Re: Memory Management question
  • Next by thread: Re: Memory Management question
  • Index(es):
    • Date
    • Thread