Should I call release on these cocoa objective-c variables?

Posted by Andrew Arrow on Stack Overflow See other posts from Stack Overflow or by Andrew Arrow
Published on 2010-04-18T14:40:09Z Indexed on 2010/04/18 14:43 UTC
Read the original article Hit count: 322

Filed under:
|

In the code below I'm making a new NSString with alloc and initializing it with the contents of some file. Because I'm calling alloc I know it's my responsibility to call release on the string when I'm done. But what about the variables "lines" and "line"? Since the method "componentsSeparatedByString" does not start with the word "new" or "create" can I assume "lines" will be autoreleased? Same question for "line" since "objectAtIndex" also does not start with "new" or "create".

  NSString* buffer = [[NSString alloc] initWithData:[fileManager contentsAtPath:@"/foo"]
                               encoding:NSUTF8StringEncoding];

  NSArray* lines = [buffer componentsSeparatedByString:@"\n"];
  NSString* line = [lines objectAtIndex:5];

  // do something with line

  [buffer release];

So is the code above okay? Or should I be calling "release" on lines and line too? Thanks.

© Stack Overflow or respective owner

Related posts about cocoa

Related posts about objective-c