• 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: Two basic questions
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Two basic questions


  • Subject: Re: Two basic questions
  • From: Izidor Jerebic <email@hidden>
  • Date: Thu, 28 Feb 2002 16:54:21 +0100

On Thursday, February 28, 2002, at 04:30 PM, David Newberry wrote:

2) Is there some reason a NSMutableString should not be returned from a string? Any time I try it seems to cause problems. Am I maybe doing something else wrong, or is there something inherently wrong with doing this? Returning an NSString seems to be fine.

Can you clarify this? What do you mean by "returned from a string"? It might help to see an example from your code, and what it does as opposed to what you want it to do.

Acks, oops. Sorry, I meant; "returned from a function". Sorry about that all.

Eg, I'll do something like this:

- (NSString *)myFunc
{
NSMutaleString *string = [NSMutableString stringWithCapacity:0];
[string appendString:@"test"];
return string;
}

And then later when I try to use the string that was returned from this function, I'll get an error relating to malloc. A double-freeing, as I recall. The problem doesn't occur if I use an NSString and then use NSString stringByAppendingString:.



The above code should cause no problems, but the resulting string is autoreleased and is valid only during event loop. If stored somewhere and used after several events, it will cause memory errors. Constant NSStrings are never deallocated, so if you return constant string, your error is masked, because the result is never deallocated, although it is released.

You should retain or copy the result of this method if you need it for longer period. This is basic memory management in Cocoa.

izidor
_______________________________________________
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: Two basic questions (From: David Newberry <email@hidden>)

  • Prev by Date: Re: Two basic questions
  • Next by Date: Re: How can I hide an NSView properly?
  • Previous by thread: Re: Two basic questions
  • Next by thread: Re: Two basic questions
  • Index(es):
    • Date
    • Thread