Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7025093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:55:43+00:00 2026-05-27T23:55:43+00:00

I use Xcode 4. Here’s a huge memory leak that I suppose is one

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:55:44+00:00Added an answer on May 27, 2026 at 11:55 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Getting an error message Nested functions are disabled, use -fnested etc in XCode/ObjC. Here's
My xcode version is 4.2. When I use property to generate getter/setter methods. Here
I could use a little clarity here. I've opened a project that is a
I am looking for resources (especially video) to show how to use Xcode's interface
I would like to use Apple's Xcode IDE to develop applications on the OS
I'd like to use gdb or Xcode's debugger to watch every message sent to
In Xcode, I can use CMD - R to run (or CMD - Y
I use MacVim as my editor within Xcode. By default Command-B and :make are
I use a statically linked library for Sqlite in an iPhone Xcode project. I
In an Xcode project, if I use std::cout to write to the console the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.