Search code examples
objective-ccocoamemory-managementnsstringnsmutablestring

Memory management and ownership when returning an NSString from a method


I'm writing a method in Objective-C which is to return a string based on an NSMutableArray input. I don't intend for the callers to modify the string but couldn't care less either. The method's current signature reads as:

- (NSMutableString *) generateString:(NSMutableArray *)myArray

The project is currently a command-line Foundation Tool in order to learn the ropes but this will ultimately end up in an iPhone application.

My questions are:

  1. Would it be more prudent to return an NSString?

2) When returning the NSMutableString should I use autorelease? If you're recommending I return an NSString how does that modify returning it?

3) If I also want to return an error object identifying if an error occurred, perhaps an enum and a string how does ownership work in that case? The method isn't called alloc, new or copy so how would the caller know? How would I return the allocated error object in addition to the string in the above method?

Thanks!


Solution

    1. I would return a NSString in most cases. It has a slightly smaller memory footprint and if the caller needs a mutable copy, creating one is just a method away: mutableCopy.
    2. You not only should but have to autorelease the NSString or NSMutableString your method returns (at least if you name your method like in the example)! The Memory Management Programming Guide lists rules you must follow to avoid a complete mess and explains everything in-depth. So read it!
    3. The Foundation framework uses the NSError class to describe errors. To "return" additional parameters your method would look like this:

      - (NSString *)generateString:(NSMutableArray *)myArray error:(NSError **)error
      {
          BOOL failed;
          // Do some fancy stuff, if the operation fails, failed is true
          if (failed == YES) {
              if (error != NULL) {
                  *error = [NSError errorWithDomain:@"your_domain" code:123 userInfo:nil];
              }
              return nil;
          }
      
          return [yourString autorelease];
      }
      
      // ...
      
      // Calling your method
      NSError *error = nil;
      [someObject generateString:someArray error:&error];
      

    Note the two ** and that any NSError var may be NULL and not nil (so you should check for this in the method's body).