• 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: How can I release an NSString?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How can I release an NSString?


  • Subject: Re: How can I release an NSString?
  • From: Jim Correia <email@hidden>
  • Date: Sun, 19 Jan 2003 07:57:56 -0500

On Sunday, January 19, 2003, at 04:51 AM, email@hidden wrote:

- (NSString *)giveMeAString
{
NSString *aString;

aString = [NSString stringWithString:@"Hello World!"];

return aString
}

My question is: is memory leaking in this code, because the string is never released? Should I use autorelease?

stringWithString: is returning an autoreleased NSString * so you have to do nothing.

To be pedantic, the object ownership rules don't say that stringWithString will return an autoreleased string. They only say that you as the caller didn't create, copy, or retain it, so releasing it isn't your problem. (And if you want to keep it around, you had better retain/copy it, and later release it.)

In fact, there is no reason that the particular implementation couldn't be

- (NSString *)stringWithString:(NSString *)aString
{
return aString;
}

in which case autorelease wouldn't have even entered the picture. Or it could be implemented as

return [[aString retain] autorelease];

or

return [[aString copy] autorelease];

The point is to not get caught up worrying about the implementation of whether it autoreleased the string or not, just that the contract says releasing it isn't your problem, nothing more, nothing less.

Jim
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: How can I release an NSString? (From: email@hidden)

  • Prev by Date: NSMatrix of NSView ?
  • Next by Date: Re: Getting a reference to the data model from a delegate
  • Previous by thread: Re: How can I release an NSString?
  • Next by thread: Re: How can I release an NSString?
  • Index(es):
    • Date
    • Thread