AVAudioPlayer Memory Leaks.

Discussion in 'iOS Programming' started by PizzaTray, Mar 2, 2010.

  1. PizzaTray macrumors newbie

    Joined:
    Oct 31, 2009
    #1
    Code:
    - (void) playIt:(NSString*)sndTxt andType:(NSString*)sndType {
    	
    	NSString *path = [[NSBundle mainBundle] pathForResource:sndTxt ofType:sndType];
    	theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    	theAudio.delegate = self;
    	[theAudio play];
    }
    Code:
    [self playIt:@"button-30" andType:@"mp3"];
    "Leaked Object:
    GeneralBlock-304
    AVAudioPlayer"

    Anyone know how to free the leaks causes by the avPlayer?
     
  2. PizzaTray thread starter macrumors newbie

    Joined:
    Oct 31, 2009
    #2
    Fix It :)

    If someone has the same problem here is the code:

    Code:
    AVAudioPlayer*		 theAudio;
    Code:
    @property (nonatomic,retain) AVAudioPlayer* theAudio;
    Code:
    @synthesize theAudio;
    Code:
    - (void) playIt:(NSString*)sndTxt andType:(NSString*)sndType {
    	
    	NSString *path = [[NSBundle mainBundle] pathForResource:sndTxt ofType:sndType];
    	
    	self.theAudio = [AVAudioPlayer alloc];
    	if([theAudio initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]) {
    		[theAudio autorelease];
    	}
    	else {
    		[theAudio release];
    		theAudio = nil;
    	}
    	[self.theAudio setDelegate:self];
    	
    	[self.theAudio play];
    }
    Code:
    - (void)dealloc {
    	[self.theAudio stop];
    	[self.theAudio release];
    }
    And now u've got 0 leaks :p
     

Share This Page