Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS - I'm confused how memory is being handled here?
    text
    copied!<p>UIImage API Reference Document:-<br> initWithContentsOfFile:<br> Initializes and returns the image object with the contents of the specified file.</p> <pre><code>- (id)initWithContentsOfFile:(NSString *)path </code></pre> <p><strong>Parameters</strong><br> <strong><em>path</em></strong><br> The path to the file. This path should include the filename extension that identifies the type of the image data.<br> <strong>Return Value</strong> An initialized <strong>UIImage object</strong>, or <strong>nil</strong> if the method could not find the file or initialize the image from its contents.</p> <p><br> <I> Considering this scenario, suppose I have a class, it could be extension of any class. Just took UIImage for example.</I></p> <pre><code>@interface myImage : UIImage { BOOL isDefaultSet; } -(id)initWithDefaultImage; @end @implementation myImage -(id)initWithDefaultImage { NSString *path = [[NSBundle mainBundle] pathForResource:@"someInvalidImage" ofType:@"png"]; idDefaultSet = YES; return [self initWithContentsOfFile:path]; } @end //somewhere in other class: NSString *path = [[NSBundle mainBundle] pathForResource:@"someInvalidImage" ofType:@"png"]; myImage *myObject = [[myImage alloc] initWithDefaultImage]; UIImage *yourObject = [[UIImage alloc] initWithContentsOfFile:path]; </code></pre> <p>now here in both cases, </p> <p>"alloc" gives "retainCount+1" <br></p> <p>and if </p> <blockquote> <p>initWithDefaultImage/initWithContentsOfFile</p> </blockquote> <p>returned nil due to some issue - lets say (invalid file path), this memory will be leaked as </p> <blockquote> <p>myObject/yourObject</p> </blockquote> <p>will be set to nil even though the <strong>allocation</strong> was made before <strong>init</strong>.</p> <p>I have seen many implementations for extended classes/interfaces in this manner. I'm confused how memory is being handled here? can anyone share view on this?</p>
 

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