views:

199

answers:

2

In my iPhone Project when i select build and analyze (shift + mac + A ) it will give me all potential memory leak in my project... but in my current project it is not working... when i intentionally put a memory leak and select build and analyze... it doesn't give me any potential memory leak as analyzer result

if i write

NSMutableArray *tempArray = [[NSMutableArray alloc] initWithCapacity:6];
NSMutableArray *tempArrayfinal = [[NSMutableArray alloc] initWithCapacity:12];

and doesn't release it... it is not giving me any potential leak but if i write

NSString *leakyString = [[NSString alloc] initWithString:@"Leaky String "];
NSLog(@"%@",leakyString);

here it gives me potential leak as analyzer result...why it is not giving potential leak in NSMutableArray and why it gives potential leak in NSString ?... how can i rely on Build and analyze for memory leak...

I've run the app with Leak tool and it is showing me leak in tempArray and tempArrayfinal

Here is my function

- (void)viewDidLoad 
{
    [super viewDidLoad];

    maxOnSnaxAppDelegate *delegate = (maxOnSnaxAppDelegate *)[[UIApplication sharedApplication] delegate];
    lblMessage.tag = MESSAGE_LABEL;
    lblGuess.tag = GUESS_LABEL;
    lblScore.tag = SCORE_LABEL;
    self.gameCompleteView.tag = FINAL_SCORE_VIEW;
    lblFinalScore.tag = FINAL_SCORE_LABEL;

    lblGuess.text = @"";
    lblMessage.text = @"";
    lblScore.text = @"";

    int row = 0;
    int column = 0;

    maxImagrArray = [[NSMutableArray alloc] init];

    for(UIView *subview in [self.view subviews]) {

        if([subview isKindOfClass:[CustomImageView class]])
        {
            [subview removeFromSuperview];
        }
    }

    for(int i = 0 ; i < 12 ; i++)
    {
        if(i%3 == 0 && i != 0)
        {
            row++;
            column = -1;
        }
        if(i != 0 )
        {
            column++;
            //row = 0;
        }
        CustomImageView *tempImageView = [[CustomImageView alloc] initWithImage:[UIImage imageNamed:@"max-img.png"]];


        tempImageView.frame = CGRectMake((column*tempImageView.frame.size.width) + 45, (row*tempImageView.frame.size.height)+ 60, tempImageView.frame.size.width, tempImageView.frame.size.height);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextClearRect(context, tempImageView.bounds);
        [self.view addSubview:tempImageView];
        tempImageView.tag = i+1;
        tempImageView.userInteractionEnabled = YES;
        [maxImagrArray addObject:tempImageView];

        [tempImageView setIndex:i]; 

        [tempImageView release];
    }

    NSMutableArray *tempArray = [[NSMutableArray alloc] initWithCapacity:6];
    NSMutableArray *tempArrayfinal = [[NSMutableArray alloc] initWithCapacity:12];
    for(int i = 0 ; i < 12 ; i++)
    {
        if(i < 6)
        {
            int temp = (arc4random() % 10) + 1;
            NSString *tempStr = [[NSString alloc] initWithFormat:@"%d",temp];
            [tempArray insertObject:tempStr atIndex:i];
            [tempArrayfinal insertObject:tempStr atIndex:i];
            [tempStr release];

        }
        else
        {
            int temp = (arc4random() % [tempArray count]);
            [tempArrayfinal insertObject: (NSString *)[tempArray objectAtIndex:temp] atIndex:i];
            //int  index = [(NSString *)[tempArray objectAtIndex:temp] intValue];
            [tempArray removeObjectAtIndex:temp];

        }
        CustomImageView *tmpCustom = [maxImagrArray objectAtIndex:i];
        tmpCustom.frontImageIndex = [(NSString *)[tempArrayfinal objectAtIndex:i] intValue];
        NSLog(@"%d",tmpCustom.frontImageIndex);
    }
    [maxImagrArray release];
    delegate.time = 60.0;

    timer = nil; 

    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    delegate.view = self.view;

    //[tempArray release];
    //[tempArrayfinal release];//these 2 lines are deliberately commented to see
//...it is not showing any  potential memory leak though....
    delegate.viewController = self;

}

please help...

A: 

You shouldn't really rely on the Build and Analyze tool. It does not always catch all leaks. Threre is no substitute for following the memory management rules from apple (apart from garbage collection on OS X). However, I'm not sure why it isn't catching such a simple leak. I tried at line in a method and it did give me a result. Are you sure its not in an if statement, because I've found that sometimes in an if statement it won't correct you.

Tom H
hey thanks for the answer.. no it is not in any if statement it is in viewDidLoad method.. is it specific to array...?
mihirpmehta
Can you post the entirety of the viewDidLoad method so we can see, because I tried and it did come up in the build and analyse checks.
Tom H
i have added whole viewDidLoad.... Thanks...
mihirpmehta
I can't think why that would be. If you comment out everything but the 2 lines in question, do you get an analyzer result? If so, try commenting stuff back in gradually until you discover what it is. However, you may not want to bother, as there is no problem running it, is there?
Tom H
There is absolutely no problem in running the program... i am just concerned about the reliability of build and analyze... i want to know exact limitation of build and analyze...I'll try that... Thanks
mihirpmehta
[tempArray insertObject:tempStr atIndex:i]; [tempArrayfinal insertObject:tempStr atIndex:i]; are the culprits... when i uncomment these 2 lines.. it stops giving me the warning... i don't know why...
mihirpmehta
The analyser was never designed to catch every memory management error. I wonder why it trips up there?
Tom H
+1  A: 
[tempArray insertObject:tempStr atIndex:i];
 [tempArrayfinal insertObject:tempStr atIndex:i];

are the culprits... when i uncomment these 2 lines.. it stops giving me the warning... i don't know why...

mihirpmehta