I have a simple meth开发者_如何学Cod to read a string and parse it to an array,
-(NSArray *) readFileToArray: (NSString *)file{
NSString *values = [NSString stringWithContentsOfFile: file];
NSArray *tokens = [values componentsSeparatedByString:@":"];
return tokens;
}
however instruments did report me I got a leak on NSString at line
NSArray *tokens = [values componentsSeparatedByString:@":"];
I have no idea why this happens,
1). I think both values and tokens are autoreleased? Am I right? 2). I tried to release values and tokens(just a try), it crashes.Thanks for your help in advance.
Michael
The code you've posted is using correct memory management (the return value is autoreleased). Look at the code that is calling readFileToArray:
to see how it's handling the returned array.
The line that is leaked is NSString *values = [NSString stringWithContentsOfFile: file];
You need to add autorelease in this line to fix the leak.
精彩评论