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 ツールを使用してアプリを実行しましたが、リークが表示され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;

}

助けてください...

4

2 に答える 2

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

犯人です...これらの2行のコメントを外すと..警告が表示されなくなります...理由がわかりません...

于 2010-06-15T06:19:04.247 に答える
0

Build and Analyze ツールにあまり頼るべきではありません。常にすべてのリークをキャッチできるわけではありません。Apple のメモリ管理規則に従うことに代わるものはありません (OS X のガベージ コレクションは別として)。ただし、なぜこのような単純なリークをキャッチできないのかはわかりません。メソッドの行で試してみたところ、結果が得られました。if ステートメントでは修正されない場合があるため、if ステートメントではありませんか。

于 2010-06-10T06:41:33.453 に答える