3

在我的 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 中产生潜在泄漏?...我如何依赖构建和分析内存泄漏...

我已经使用 Leak 工具运行了该应用程序,它显示我正在泄漏tempArray并且tempArrayfinal

这是我的功能

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

}

请帮忙...

4

2 回答 2

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

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

于 2010-06-15T06:19:04.247 回答
0

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

于 2010-06-10T06:41:33.453 回答