2011-11-08 5 views
0

これはシミュレータの問題であるか何かが欠落しているかどうかわかりませんが、theAudioはdeallocのように解放されますが、次のコードではメモリリークが発生します。私は.wavファイルを再生しており、サウンドの値は整数の値に応じて変化します。アプリOK実行が、私は任意の提案が大幅に高く評価されメモリリークの原因となるAVAudioPlayer

Xcodeでこれをテストwhwn 16ビットリークはフラグが設定されている:

答えて

0
#import <UIKit/UIKit.h> 
#import <AVFoundation/AVFoundation.h> 

@interface Water : UIViewController 
<AVAudioPlayerDelegate> 

{ 

} 

@property (nonatomic,retain) AVAudioPlayer *theAudio; 


-(IBAction) goButMenu: (id) sender; 
-(IBAction) goDrinkMenu: (id) sender; 
-(IBAction) playwater; 


@end 



@implementation Water 
@synthesize theAudio; 

-(IBAction) goDrinkMenu: (id) sender{ 

ButDrinkMenu *butdrinkmenu1 = [[ButDrinkMenu alloc] initWithNibName:nil bundle:nil]; 
[self presentModalViewController:butdrinkmenu1 animated:YES]; 

} 

-(void) viewDidLoad { 

if (BoyOrGirl == 1) { 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Applause" ofType:@"wav"]; 
    theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

else { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"bongo" ofType:@"wav"]; 

    theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 

} 





} 

-(IBAction) playwater{ 

if (BoyOrGirl == 1) { 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Applause" ofType:@"wav"]; 
    theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 




} 

else { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"bongo" ofType:@"wav"]; 
    theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 

} 


} 

は間違いなく直接デバイス上でテストします。シミュレータでリークをテストすると、不正確な結果が返されることがあります。

つまり、このコードサンプルにはいくつかのメモリ関連の問題があります。ここであなたのdeallocメソッドを含めることができますが、いくつかの洞察を提供するかもしれませんが、ここに問題があります(割り当てのいくつかに表示されます)。

あなたのAVAudioPlayerは、あなたのプロパティに割り当てられていないか、範囲内。理想的には割り当てがより多くのようになりますプレーヤー:あなたは直接プレイヤーを割り当てるので

theAudio = [[AVAudioPlayer alloc] initWith... 

:直接インスタンス変数をプレーヤーを割り当て、あなたがそれを実現してきた方法とは対照的に

AVAudioPlayer *myPlayer = [[AVAudioPlayer alloc] initWithContents.... 
[myPlayer setDelegate:self]; 
[self setTheAudio:myPlayer]; 
[myPlayer release]; 

、インスタンス変数は、あなたのプレーヤーは保持されていません。これは、あなたの呼び出しを適切に試行し、漏れの原因となる場合は、早すぎるリリースと不明瞭なポインタのどちらかにつながる可能性があります。

+0

ありがとう、私は簡単なテストを行っており、このコードはリークを解決しました、私はいくつかのテストを行う必要がありますが、よく見えます。乾杯。 – TJ15

+0

素晴らしい!答えがうまくいけば、それを受け入れるように選択してください。 – isaac

関連する問題