Search code examples
xcode4memory-leaksstatic-analysis

Static analyzer stopped detecting unreleased allocations


I use Xcode 4. Here's a huge memory leak that I suppose is one of the easiest hunts for static analyzer:

-(void)leak:(id)sender{
 images=[[NSMutableArray alloc] init];
 for (int i=0; i<=2000; i++) { 
     UIImage *image=[[UIImage alloc] initWithContentsOfFile:[[NSBundle   
 mainBundle]pathForResource:@"Icon" ofType:@"png"]];
     [images addObject: image];
 }
}

But when I select Product|Analyze the analyzer does not catch anything. Have I done any unintended changes to the Xcode? Or is this the way it really should be?


Solution

  • OK, here's what I got after trying many variants. In the code above, in the question, I allocate an image and then I put the image which has a retain count of +1 to the images array, which in turn also retains the image, thus resulting in the retain count of +2. Ideally I had to release the image object after adding it to the array. But I don't, which is an obvious memory leak. But weirdly static analyzer can't catch this leak. Now, here's the catch. The code is in for loop. If I place it outside the loop, the analyzer detects it. So I think it's something related with compiling optimization that doesn't notice danger inside a loop.

    And here's another issue. Though the analyzer can detect the leak, if I test it on instruments the leak will still not be detected. Also weird, isn't it? Ok, here's why. It's because instruments does not show leaks if there's any reference to an object. So I'm giving you two cases to compare:

      images=[[NSMutableArray alloc] init]; 
      UIImage *image=[[UIImage alloc] initWithContentsOfFile:[[NSBundle   
      mainBundle]pathForResource:@"Icon" ofType:@"png"]];
      [images addObject: image];
    

    This way the static analyzer will catch the memory leak (notice the code is not inside a loop). But the instruments will not. But if I add one more line of code :

      images=[[NSMutableArray alloc] init]; 
      UIImage *image=[[UIImage alloc] initWithContentsOfFile:[[NSBundle   
      mainBundle]pathForResource:@"Icon" ofType:@"png"]];
      [images addObject: image];
    
      [images release];
    

    Then there will be no object owning the image object so it'll be floating around. This way the instruments will also detect the leak.

    I know the case above is almost impossible in a real world application, but is an example for demonstration purposes so that you do not get confused.

    P.S. I actually did not try to disable the compiling optimization (if it exists off course.). But I've seen a video where memory leak inside a loop is also detected by static analyzer. Go search for it if you're interested.