Note that there are some explanatory texts on larger screens.

plurals
  1. POiPhone Memory Management with Properties of Singletons
    primarykey
    data
    text
    <p>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. </p> <p>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. </p> <p>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. </p> <p>UPDATE: articleString is defined as follows in the header:</p> <pre><code>@property (nonatomic, retain) NSMutableString *articleString; </code></pre> <p>the parseArticle method below is a placeholder for a series of methods that manipulate articleString. </p> <pre><code> self.articleString = [[NSMutableString alloc] initWithData:articleData 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]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload