Note that there are some explanatory texts on larger screens.

plurals
  1. POPotential Leak of an Object that is released before reuse and dealloc'd on exit
    text
    copied!<p>Using the xcode analyzer, I am getting a warning for a potential leak of an object. This warning is perplexing and I need some explanation why I am getting this error. Here is the code in which mediaSources hold the pointer to the object in question:</p> <p>In the .h file, the a pointer to the MediaSources class is created and given a retain property:</p> <pre><code>@interface RootViewController : UIViewController &lt;...&gt; { ... MediaSources *mediaSources; ... } @property (nonatomic, retain) MediaSources *mediaSources; </code></pre> <p>In the .m file (rootViewController) is a method that can be called many times. Consequently, I release the object on each entry and alloc a new object. The MediaSources object performs background tasks, so I don't want to release it until I know it is complete. If I use autoRelease on the line that the class is alloc'd in, it crashes. :</p> <pre><code>-(void) getSelectedMediaSources { [self setMediaSources: nil]; // release old stuff and nilify [self setMediaSources: [[MediaSources alloc] init]]; [self.mediaSources checkForMediaSourceUpdates]; } </code></pre> <p>Also in the .m file, mediaSources is also synthesized and is released in dealloc</p> <pre><code>@synthesize mediaSources; ... - (void)dealloc { ... [mediaSources release]; ... [super dealloc]; } </code></pre> <p>Please explain why I am getting this warning. I don't see how there could be a leak. Dealloc should release the last copy of this object.</p> <p>IN RESPONSE TO REQUEST FOR CODE FROM checkForMediaSourceUpdates. This is going to get a bit complicated, but below is the essence: </p> <pre><code>(void) checkForMediaSourceUpdates { NSString *s = [NSString stringWithFormat:@"http://www.mywebsite.com/mediaSources/%@/mediaSources.plist", countryCode]; NSURL *url = [NSURL URLWithString:s]; NSURLRequest *req = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60.0]; MyDownloader *d = [[MyDownloader alloc] initWithRequest:req]; [self.connections addObject:d]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkForMediaSourceUpdatesDownloadFinished:) name:@"connectionFinished" object:d]; [d.connection start]; [d release]; } -(void) checkForMediaSourceUpdatesDownloadFinished: (NSNotification *) n { MyDownloader *d = [n object]; NSData *data = nil; if ([n userInfo]) { NSLog(@"In checkForMediaSourceUpdatesDownloadFinished: MyDownloader returned an error"); } else { data = [d receivedData]; // do something with the data } } </code></pre> <p>The myDownloader class performs a download of a file as specified in the input NSURLRequest. After completing the download, this class will generate a NSNotification named "connectionFinished". The user of this class must register for this notification and handle all cleanup operations of this class. If the download fails, this class will generate a NSNotification, also named "connectionFinished", but with the addition of userInfo that indicates an error occured. Again, the user of this class must register for this notification and handle all cleanup operations of this class.</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