Memory management issues with retrieving particular lines of text from string

两盒软妹~` 提交于 2019-12-13 07:39:10

问题


I am kind of new to iOS development.

I want to retrieve strings from the text(.rtf) file I have. The file is within my application main bundle. It's content are:

#start word1 First word end word2 Second word end //lots of things to be added later

Code:

 path = [[NSBundle mainBundle]pathForResource:@"words" ofType:@"rtf"];
    if(path)
    {
        NSLog(@"path exists");
    }
    NSError *error = nil;

    NSString *file = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
    if(error)
    {
        NSLog(@"error");
    }
    NSString *finalword= [[NSString alloc]init ];
    NSString *startfrom = [[NSString alloc] initWithFormat:@"word%i",i+1];
    i++;
    NSLog(@"%@",startfrom);
    NSString *wordoftheday = [[NSString alloc]init ];
    NSScanner *scanner = [NSScanner scannerWithString:file];
    [scanner scanUpToString:startfrom intoString:nil];
    [scanner scanUpToString:@"end" intoString:&wordoftheday];
    finalword = [wordoftheday substringFromIndex:[startfrom length]];
    NSLog(@"%@",finalword);
    Word.text = final word;  //change label text
//[final word release];
//[wordoftheday release];
//[file release];

Code is working fine but it leaves me with memory management issues. App crashes if I release the variables in the last commented code.

Also this method is in my viewdidload. I want the label to change text when user click a button. I will have to write the same code again in that method which leaves me with more memory issue.


回答1:


So looking at these one by one, focusing on the memory issues and not the overall strategy here:

NSString *finalword= [[NSString alloc]init ];

Here you alloc/init a new immutable and empty NSString, and then you end up overwriting the pointer to this later anyway. You should just delete this line. And then you'll need to move the declaration down a few lines to this:

NSString *finalword = [wordoftheday substringFromIndex:[startfrom length]];

Then you have:

NSString *startfrom = [[NSString alloc] initWithFormat:@"word%i",i+1];

This one you need to release later. Or just change it to:

NSString *startfrom = [NSString stringWithFormat:@"word%i",i+1];

Then you have:

NSString *wordoftheday = [[NSString alloc]init ];

Same story as finalword. Except that you do need to define this variable so you can pass it to the scanner later. So change it to:

NSString *wordoftheday = nil;

And lastly, you can release 'file'. That is fine. But you don't want to release 'wordoftheday' or 'finalword' because you don't own those strings. You did not create them yourself.

And one other note:

if(error)

That is not the correct way to check for an error in loading 'file'. You should check the return from the method and then look for an error if and only if the return value was nil. So change that line to:

if(!file)

(OK, that wasn't really a memory issue, but a bug I did notice.)

I think that's all of it at least as far as memory issues. I hope that helps.




回答2:


make those variables as member variables and release in the dealloc



来源:https://stackoverflow.com/questions/8484826/memory-management-issues-with-retrieving-particular-lines-of-text-from-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!