• 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: Some confusion on memory management
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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
References: 
 >Some confusion on memory management (From: Mike Abdullah <email@hidden>)

  • Prev by Date: Re: Some confusion on memory management
  • Next by Date: Re: Some confusion on memory management
  • Previous by thread: Re: Some confusion on memory management
  • Next by thread: Re: Some confusion on memory management
  • Index(es):
    • Date
    • Thread