开发者

iPhone Memory Management with Properties of Singletons

I seem to have a fundamental gap in my memory management understanding. The code below is located within a singleton that gets called multiple times within my app to parse data that is downloaded from the web. For each article I download, I allocate a mutable string, then do tons of parsing, then write the file to the file system for later display in a UIWebView.

But every time I enter this method, I allocate a new "articleString". And I never release this string. I think this is a leak, but if I add a release at the bottom of this method (after the file is written), my app crashes the next time this method is called. I don't understand why it crashes, since another NSMutableString is allocated next time it is called.

UPDATE: I do release articleString in the dealloc method. But it still seems that I should release at the end of this method, since I alloc every time I enter.

UPDATE: articleString is defined as follows in the header:

@property (nonatomic, retain) NSMutableString *articleString;

the parseArticle method below is a placeholder for a series of methods that manipulate articleString.

 self.articleString = [[NSMutableString alloc] initWithData:artic开发者_如何转开发leData encoding:NSUTF8StringEncoding];

 //Parse the article for display
 [self parseArticle];

 //Write the article string to a file for later display
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *path = [documentsDirectory stringByAppendingPathComponent:@"article.html"];
 NSLog(@"%@", articleString);
 [articleString writeToFile:path atomically:YES];


I like to let properties handle this for me. If the articleString property is set to retain then this is simple.

self.articleString = [[[NSMutableString alloc] initWithData:articleData encoding:NSUTF8StringEncoding] autorelease];

[self doStuff];

Then

- (void)dealloc {
    self.articleString = nil;
    [super dealloc]
}

article string will get released and properly retain when you set a new one. And it will be cleaned up on dealloc.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜