IPhone programming: avaudioplayer game memory leaks

I'm new to using avadioplayer and I seem to have memory when I play sound. I can't figure out what I'm missing to get rid of it in Tool. could this be false?

ViewController.h:

@interface ISpectatorViewController : UIViewController <UIAccelerometerDelegate>{

AVAudioPlayer *massCheerSoundID;

}

@property(nonatomic,retain) AVAudioPlayer * massCheerSoundID;

      

//ViewController.m

- (void)viewDidLoad {

    NSString * filePath;

    filePath = [[NSBundle mainBundle] pathForResource:@"massCheer" ofType:@"mp3"];

    massCheerSoundID = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filePath ]error:nil];

}

- (void) playSound
{

        if(massCheerSoundID.playing == false)
    {

        massCheerSoundID.currentTime = 0.0;


                //leak here
        [massCheerSoundID play];
    }
}

- (void)dealloc {

    [super dealloc];
    [massCheerSoundID release];

}

      

I figured out what the problem is.

I need to add AVAudioPlayerDelegate to the interface as I have set UIAccelerometerDelegate instead

@interface iSpectatorViewController: UIViewController<AVAudioPlayerDelegate>

      

and install

massCheerSoundId.delegate = self

      

+2


source to share


5 answers


Try adding MediaPlayer.framework to your project with no code changes



+3


source


Found the same leak today. Adding MediaPlayer.framework seems to fix this.



+2


source


Could this be your method dealloc

?

I think the agreement is first release

and then calls [super dealloc]

.

I always assumed this was intended to "first release your stuff and then process its parent class".

Your method dealloc

should read:

- (void)dealloc {

    [massCheerSoundID release];
    [super dealloc];

}

      

+1


source


Found the same leak today. Adding MediaPlayer.framework seems to fix this.

It worked for me too. It's strange.

+1


source


The problem might be that you are rerunning massCheerSoundId over and over if you aren’t rolling your mind. So, if you add your subview, the viewDidLoad callable gets called and you initialize your player, then maybe you remove the subview and add it again, since you haven't released the view (if you only have one copy) dealloc doesn't get called (I so don't think so anyway), then if you add the view again, then massCheerSoundId will get overflow and leak .. Another problem might be that since you have massCheerSoundId as a property, you allocate it and the grid creator also saves it, so then you are left with the +2 refcount when it should only be +1 ... so assuming my assumptions are correct, I would suggest writing viewDidLoad.like this

    - (void)viewDidLoad {    
    NSString * filePath;   
     filePath = [[NSBundle mainBundle] pathForResource:@"massCheer" ofType:@"mp3"]; 
    if(massCharSoundId!=nil)
{
       AVAudioPlayer *p= [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURLfileURLWithPath:filePath ]error:nil];}
       massCheerSoundID = p;
     [p release];
}

      

0


source







All Articles