How can I release an array created from a parameter?
I have function like
-(NSMutableArray*)composePhrase:(NSString*) phraseLiteral{
...
NSArray* wordLiterals=[phraseLiteral componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"[]"]];
...
[wordLiterals release];
}
and I always got problem with this release. Can anyone tell me how to make it right?
You need to understand the Object Ownership Policy.
You only gain the ownership automatically when the method name contains
alloc,neworcopy. Here,componentsSperatedByCharactersInSet:does not. Therefore, the returnedwordLiteralsis not owned by you. It’s autoreleased. You shouldn’treleaseit. It’s released automatically when the autorelease pool is drained when the current event loop is complete.If you want to keep the object, you
retainit. Then you own it. When you no longer needs it, youreleaseit.