5

私は、どの関数が割り当て解除されていないかを見つけようとしてLeaksをいじり回しており(私はまだこれに慣れていません)、経験豊富な洞察を実際に使用できます。

私は犯人のように見えるこのコードを持っています。このコードを呼び出すボタンを押すたびに、32kbのメモリがメモリに追加で割り当てられ、ボタンを離してもそのメモリの割り当ては解除されません。

私が見つけたのはAVAudioPlayer、m4aファイルを再生するために呼び出されるたびに、m4aファイルを解析する最後の関数はでMP4BoxParser::Initialize()あり、これにより32kbのメモリが割り当てられます。Cached_DataSource::ReadBytes

私の質問は、ボタンが押されるたびに32kbが割り当てられないように、終了後に割り当てを解除するにはどうすればよいですか?

あなたが提供できるどんな助けも大歓迎です!

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

//stop playing
theAudio.stop;


// cancel any pending handleSingleTap messages 
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(handleSingleTap) object:nil];

UITouch* touch = [[event allTouches] anyObject]; 


NSString* filename = [g_AppsList objectAtIndex: [touch view].tag];

NSString *path = [[NSBundle mainBundle] pathForResource: filename ofType:@"m4a"];  
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];  
theAudio.delegate = self; 
[theAudio prepareToPlay];
[theAudio setNumberOfLoops:-1];
[theAudio setVolume: g_Volume];
[theAudio play];
}
4

2 に答える 2

2

Cocoaでのメモリ管理の秘訣は、への呼び出しallocretainまたはcopyその後のへの呼び出しのバランスを取ることreleaseです。

この場合、変数allocを初期化するために送信していますが、を送信することはありません。theAudiorelease

-touchesBegan一度に1つのサウンドしか再生しないと仮定すると、これを行うための最良の方法は、コントローラー(このメソッドを持つプロパティ)のプロパティを使用することです。プロパティ宣言は次のようになります。

@property (nonatomic, retain) AVAudioPlayer * theAudio;

次に、メソッドでに設定theAudioする必要があります。nilinit

theAudio = nil; // note: simple assignment is preferable in init

deallocそして、必ずメソッドで変数を解放してください。

[theAudio release];

今、あなたtouchesBeganはこのように見えるかもしれません:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    //stop playing
    theAudio.stop;
    ...
    AVAudioPlayer * newAudio = [[AVAudioPlayer alloc] initWithContentsOfUrl:...];
    self.theAudio = newAudio; // it is automatically retained here...

    theAudio.delegate = self; 
    [theAudio prepareToPlay];
    [theAudio setNumberOfLoops:-1];
    [theAudio setVolume: g_Volume];
    [theAudio play];

    [newAudio release];       // ...so you can safely release it here
}
于 2010-04-14T02:50:04.817 に答える
1

この行は私には犯人に見えます:

theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];  

このリソースはいつリリースされますか?

于 2010-04-14T02:49:17.533 に答える