• 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: j o a r <email@hidden>
  • Date: Mon, 27 Mar 2006 23:55:55 +0200


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

I have a memory management question. A number of my objects have methods which return string representations of the objects.

While there's nothing wrong with doing this, you might want to consider using the archiving and serialization patterns suggested by Cocoa:


<http://developer.apple.com/documentation/Cocoa/Conceptual/Archiving/ index.html>

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.


One last question.  If extraDataArray is large should I have a local
autorelease pool which I setup just outside this loop?

You might, but only do it if you see (using some performance tool) that you have to. Be wary of premature optimizations, it's one of the programmer cardinal sins.


j o a r


Ps. Note that you say "return autoreleased", when what you really mean is "returns something that I don't have memory manage". We often use the term like you do - at least from time to time - but it's not entirely correct. In most cases the returned object will be autoreleased of course, but it doesn't have to be (it could be a singleton, or returned from a shared pool or something similar). Focus on the key feature: It's something that you don't have to memory manage.


Attachment: smime.p7s
Description: S/MIME cryptographic signature

 _______________________________________________
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: Paul Lynch <email@hidden>
References: 
 >Memory Management question (From: Bruce Truax <email@hidden>)

  • Prev by Date: About AsyncSocket class
  • Next by Date: Re: 1.0 divided by 10 = 0.10000000000000001?
  • Previous by thread: Memory Management question
  • Next by thread: Re: Memory Management question
  • Index(es):
    • Date
    • Thread