• 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 mgmt in convenience and accessor methods
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: memory mgmt in convenience and accessor methods


  • Subject: Re: memory mgmt in convenience and accessor methods
  • From: "Clark Cox" <email@hidden>
  • Date: Tue, 29 May 2007 12:32:40 -0700

On 5/29/07, Daniel Child <email@hidden> wrote:
If you shouldn't autorelease twice, then where should the autorelease
be to make sure that the variable gets safely kept in the pool?

You're over-thinking the problem. The current autorelease pool will not be drained while either of your methods are being executed (unless you explicitly drain it yourself).

I am
guessing it should be in the simpler method (newThing) since the more
complex one will then automatically receive an autoreleased instance.
(Otherwise it may be released before the more complex one uses it?)
But will the object stay around in memory after it is return from
newThingWithStg?

Yes, it will stay around (at least) until the current pool is drained (that's the whole point of autorelease). Just follow the rules and you'll be fine. From <http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Tasks/MemoryManagementRules.html>: "This is the fundamental rule:

You take ownership of an object if you create it using a method whose
name begins with "alloc" or "new" or contains "copy" (for example,
alloc, newObject, or mutableCopy), or if you send it a retain message.
You are responsible for relinquishing ownership of objects you own
using release or autorelease. Any other time you receive an object,
you must not release it."

So, following the rules: (BTW, using newXXX as the name for a method
like this is generally a bad idea; because it conflicts with the rule
stated above, I've renamed them for you):

+(id)thing {
   id newThg = [[self alloc] init];

   /* I created it (by calling +alloc), so I am responsible for releasing it */
   return [newThg autorelease];
}

+(id)thingWithStg: (int)val {
   id newThg = [Thing thing];
   [newThg setInstVar: val];

   /* I didn't create it (+newThing did) so I am not responsible for
releasing it */
   return newThg;
}


-- Clark S. Cox III email@hidden _______________________________________________

Cocoa-dev mailing list (email@hidden)

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


  • Follow-Ups:
    • Re: memory mgmt in convenience and accessor methods
      • From: Daniel Child <email@hidden>
References: 
 >memory mgmt in convenience and accessor methods (From: Daniel Child <email@hidden>)
 >Re: memory mgmt in convenience and accessor methods (From: "Clark Cox" <email@hidden>)
 >Re: memory mgmt in convenience and accessor methods (From: Daniel Child <email@hidden>)

  • Prev by Date: Re: [ANN] MacFUSE talk at Google
  • Next by Date: Re: Disabling items in NSTabView
  • Previous by thread: Re: memory mgmt in convenience and accessor methods
  • Next by thread: Re: memory mgmt in convenience and accessor methods
  • Index(es):
    • Date
    • Thread