AVAudioPlayer causa pérdida de memoria

Not sure if this is a simulator issue or I am missing something, the following code gives me a memory leak although theAudio is released as in dealloc. I am playing a .wav file and the sound will change depending on the value of an integer. The App runs ok but 16 bit leak is flagged whwn I test this in xcode

Any suggestion is greatly appreciated:

#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];

}


}

preguntado el 08 de noviembre de 11 a las 14:11

1 Respuestas

Definitely test on the device directly; testing for leaks with the simulator may return inaccurate results.

That said, there are several memory-related concerns with this code sample. You may wish to include your dealloc method here as that may offer some insight, but here's a problem (that appears in several of your allocations):

Your AVAudioPlayer isn't being assigned to your property, nor is it being released in scope. Ideally a player allocation would look more like:

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

As opposed to how you've implemented it, which assigns the player directly the instance variable:

theAudio = [[AVAudioPlayer alloc] initWith...

Because you assign the player directly to your instance variable, your player isn't being retained. This is likely to result in either premature release and a dangling pointer if you try and balance your calls properly, or it will result in leaking.

respondido 08 nov., 11:19

Thank you, I have done a quick test and this code resolved the leak, I need to do some more testing but looks good. Cheers. - TJ15

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.