2

プロジェクトのコーディングは完了しましたが、ソース コードをクライアントに提出したときにテストを行ったところ、メモリ リークが検出されました。でテストしましたInstruments using Leaks。私が問題を抱えているのは、私のものと私のもの、そして私のものAVPlayerです。これについて代替を見つける必要がありますか?または私のコードに何か問題がありますか?AVAudioPlayerAppDelegate

以下は私のコードです(ARCちなみに使用しています):

---> AppDelegate

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([RootViewAppDelegate class]));
    }
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{  
[window makeKeyAndVisible];

---> AVPlayer

self.moviePlayer = [AVPlayer playerWithURL:[NSURL fileURLWithPath:moviePath]];

---> AVAudioPlayer

NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:@"/Normal.wav"];
NSLog(@"Path to play: %@", resourcePath);
NSError* err;

//Initialize our player pointing to the path to our resource
BGMplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:resourcePath] error:&err];

if( err ){
    //bail!
    NSLog(@"Failed with reason: %@", [err localizedDescription]);
}
else{
    //set our delegate and begin playback
    BGMplayer.delegate = self;
    [BGMplayer play];
    BGMplayer.numberOfLoops = -1;
    BGMplayer.currentTime = 0;
    BGMplayer.volume = 1.0;
} 

上記のものを検出する方法は次のとおりです。

ここに画像の説明を入力


誰かが私を助けてくれることを願っています.メモリリークを検出するのはこれが初めてです。では、ご指導よろしくお願いします。どうもありがとうございます。

4

3 に答える 3

0

私の推測では、

//set our delegate and begin playback
BGMplayer.delegate = self;

AVPlayer はビューを保持し、ビューも AVPlayer を保持するため、両方がアークによって解放されるのを防ぐ保持サークルを取得します。

デリゲートが不要になったら、デリゲートを nil に設定することをお勧めします...おそらくremoveFromSuperviewビューで上書きします。

以下をビューに追加します。

- (void)removeFromSuperview {
    BGMplayer.delegate = nil;
    [super removeFromSuperview];
}
于 2012-09-25T12:19:54.707 に答える
0

まず最初に、Shift+Command+B キーの組み合わせでコードを分析して、メモリ リークがわかることを願っています。

于 2012-09-25T10:54:11.253 に答える
-1

インスタンス変数の一部が解放されていないようです。あなたのmoviePlayerプロパティはAVPlayerオブジェクトを保持していて、それを解放していないと思いますdealloc(おそらく他のインスタンス変数)。お役に立てれば。

于 2012-09-25T11:15:45.267 に答える