开发者

NSArray NSString memory leak

开发者 https://www.devze.com 2022-12-18 08:00 出处:网络
I have a simple meth开发者_如何学Cod to read a string and parse it to an array, -(NSArray *) readFileToArray: (NSString *)file{

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号