• 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 (passing objects to method)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory management question (passing objects to method)


  • Subject: Re: Memory management question (passing objects to method)
  • From: mmalcolm crawford <email@hidden>
  • Date: Tue, 1 Mar 2005 21:46:21 -0800


On Mar 1, 2005, at 6:36 PM, Justin Spahr-Summers wrote:

Except make sure you don't set it to 'nil' after an autorelease call.
That will get the autorelease pool to try and release a nil object.
Rather, call release (instead of autorelease), then set it to nil.

This is wrong.

Please, memory management is for the most part a straightforward subject that people tend to make difficult for themselves by thinking it more complicated than it is. There is a wealth of articles on the topic that explain it from a variety of different angles, and explain it correctly. There should typically be little reason to rehash the material in a post -- simply refer to one of the existing resources. Things are made much worse when someone makes an assertion that is subtly, or as in this case wholly, wrong.

To address the issue here:
Even if you set your own reference to an object to nil, the autorelease pool's reference continues to be valid, and the object will be correctly sent a release message when the autorelease pool is itself released.


- (void)setBlahToNil
{
    [blah autorelease];
    blah = nil;
}

This works "correctly" as the autorelease pool's reference to blah remains valid even after the instance variable reference is set to nil.

That said, this pattern is unusual, and you are encouraged to use standard accessor methods pervasively. Better than the above would be to simply invoke:

    [object setBlah:nil];


On Wed, 02 Mar 2005 00:48:33 +0100, Conor Dearden <email@hidden> wrote:
-(void)doSomething:(NSString *)aVar;
{
    [localInstanceOfVar autorelease];
    localInstanceOfVar = nil;
}

I can see little reason for a method such as this (which takes a string argument but does nothing with it). it might make sense as an action method:


-(void)doSomething:sender
{
    [localInstanceOfVar autorelease];
    localInstanceOfVar = nil;
}

but as per the caution above, this would probably be better implemented using standard accessor methods:

-(void)doSomething:sender
{
    [self setLocalInstanceOfVar:nil];
}



mmalc


_______________________________________________ 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 (passing objects to method)
      • From: Tim Lucas <email@hidden>
References: 
 >Re: Re: Memory management question (passing objects to method) (From: Conor Dearden <email@hidden>)
 >Re: Re: Memory management question (passing objects to method) (From: Justin Spahr-Summers <email@hidden>)

  • Prev by Date: Re: Help: How to show an application again after 5 days?
  • Next by Date: KVO and the description method
  • Previous by thread: Re: Re: Memory management question (passing objects to method)
  • Next by thread: Re: Memory management question (passing objects to method)
  • Index(es):
    • Date
    • Thread