Re: Some confusion on memory management
Re: Some confusion on memory management
- Subject: Re: Some confusion on memory management
- From: Jeff LaMarche <email@hidden>
- Date: Fri, 20 Jan 2006 10:24:44 -0500
On Jan 20, 2006, at 10:09 AM, Mike Abdullah wrote:
NSString *myString;
And in the two methods I have this:
- (void)setMyString:(NSString *)newString
{
myString = newString;
}
-(NSString *)myString
{
return myString;
}
There are a number of good books on Cocoa and Objective-C. I suggest
you pick one up. You need to retain your reference in your mutator,
and release the old reference. There is some debate over what the
"correct" way of doing an Objective-C mutator, but here's one method
that will work (it might not be the best way, and some people will
prefer a different way, but this is infinitely better than what
you're doing now:
- (void)setMyString:(NSString *) newString
{
if (my != newString)
{
[newString retain];
[myString release];
myString = aTheString;
}
}
If you don't retain the object, you can't be sure it will stick
around. If you don't release the old version, you're leaking memory.
_______________________________________________
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