构建和分析不在我的项目中工作

时间:2010-06-09 12:58:15

标签: iphone xcode xcodebuild

在我的iPhone项目中,当我选择构建和分析(shift + mac + A)时,它会在我的项目中给我所有潜在的内存泄漏...但是在我当前的项目中它不起作用...当我故意放内存泄漏并选择构建和分析...它不会给我任何潜在的内存泄漏作为分析器结果

如果我写

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

并且没有释放它...它没有给我任何潜在的泄漏但是如果我写

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

这里它给我潜在的泄漏作为分析器结果...为什么它没有给NSMutableArray潜在的泄漏以及为什么它在NSString中给出了潜在的泄漏?...我怎么能依赖Build和分析内存泄漏......

我使用泄漏工具运行应用程序,它在tempArraytempArrayfinal

中显示泄漏

这是我的功能

- (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;

}

请帮忙......

2 个答案:

答案 0 :(得分:1)

[tempArray insertObject:tempStr atIndex:i];
 [tempArrayfinal insertObject:tempStr atIndex:i];

是罪魁祸首......当我取消注释这两行时......它停止给我警告......我不知道为什么......

答案 1 :(得分:0)

您不应该真正依赖于构建和分析工具。它并不总能捕获所有泄漏。 Threre不能替代遵循apple的内存管理规则(除了OS X上的垃圾收集)。但是,我不确定为什么它没有抓住这么简单的泄漏。我尝试了一种方法,它确实给了我一个结果。你确定它不在if语句中,因为我发现有时在if语句中它不会纠正你。