2

私はiOS開発に不慣れです。バックグラウンドスレッドの実行中に問題が発生しました。私のコードでは、resetUiはメインUIスレッドで実行されていますが、画像データをフェッチして画像を更新するためにバックグラウンドスレッドを開始しています。すべて正常に動作しますが、performSelectorInBackgroundを呼び出すと、メモリがリークします。

どこが間違っているのか教えてください。また、URL(dataWithContentsOfURL)から取得しているときに画像を更新するためのより良い方法があるかどうかを提案してください。

[アップデート]

Instrumentは、perfromSelectorInBackgroundとUIImageimageWithDataで2つの別々のリークを表示しています。imageupdate(imageWithData)で何かがひどく間違っていると思います

-(void)updateData{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];     

    profileName.text = oAuthTwitter.screen_name;

    if(profilePic.image == nil){
        NSString *urlString = @"https://api.twitter.com/1/users/profile_image/";
        urlString = [urlString stringByAppendingFormat:oAuthTwitter.screen_name];   
        urlString = [urlString stringByAppendingFormat:@"?size=bigger"];            
        profilePic.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]];          
        [activityIndicator stopAnimating];
        [activityIndicator release];
    }   
    [pool drain];   

}   

- (void)resetUi{

    if (oAuthTwitter.oauth_token_authorized) {
        profilePic.hidden = NO;
        profileName.hidden = NO;

        NSLog(@"Resetting to authorised state");
        [self performSelectorInBackground:@selector(updateData) withObject:nil];

    }else{

        NSLog(@"Resetting Twitter UI to non-authorized state.");

    profilePic.hidden = YES;
        profileName.hidden = YES;       

    }

}   
4

1 に答える 1

3

使うべきだと思います

[pool release];

それよりも

[pool drain];

これははるかに優れた方法です。

また、メイン スレッドで activityIndi​​cator を解放してみてください。

あなたが与えたコードから、リークの他の原因を見つけることができません..リーク計測器と静的アナライザーを使用してコードを実行しようとしましたか?

于 2012-05-30T06:31:09.070 に答える