Re: Memory management question (passing objects to method)
Re: Memory management question (passing objects to method)
- Subject: Re: Memory management question (passing objects to method)
- From: Bob Ippolito <email@hidden>
- Date: Tue, 1 Mar 2005 11:18:37 -0500
On Mar 1, 2005, at 3:45 AM, Evan Schoenberg wrote:
On Mar 1, 2005, at 2:24 AM, Jiva DeVoe wrote:
You should instead do:
-(void)doSomething:(NSString *)aVar;
{
localInstanceOfVar = [aVar retain];
}
And moreover, you want to do either:
-(void)doSomething:(NSString *)aVar;
{
[localInstanceOfVar autorelease];
localInstanceOfVar = [aVar retain];
}
or
-(void)doSomething:(NSString *)aVar;
{
if(localInstanceOfVar != aVar){
[localInstanceOfVar release];
localInstanceOfVar = [aVar retain];
}
}
because you want to release the old value before retaining the new one.
Usually this is implemented as:
-(void)doSomething:(NSString *)aVar;
{
[aVar retain];
[localInstanceOfVar release];
localInstanceOfVar = aVar;
}
The autorelease version may be slightly more convenient at one less
line of code, but it's generally better not to use autorelease pools
unless there's a better reason than that.
-bob
_______________________________________________
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