2011-04-18 2 views
0

간단한 사운드 보드 앱에서 버튼을 누를 때마다 해당 사운드가 재생됩니다. 그러나 필자는 내 변수를 올바르게 릴리스하지 않는 것으로 보입니다. (또는 코드를 다시 돌려서 사운드를 재생하지만 메모리 누수가있는 곳으로 돌아 왔으므로) 지금은 앱 스토어 표준을 충족시키지 못할 것입니다. 이 문제를 해결하는 방법을 찾아야 도움이 될 것입니다.iOS AudioPlayer Simple Soundboard App에서의 메모리 누수

내 ViewController.h은 다음과 같습니다

#import <UIKit/UIKit.h> 
#import <Foundation/Foundation.h> 
#import <AVFoundation/AVAudioPlayer.h> 


@interface FirstViewController : UIViewController <AVAudioPlayerDelegate> 
{ 

} 

-(IBAction)pushTropical; 
-(IBAction)pushVakidis; 
-(IBAction)pushTwiggy; 
-(IBAction)pushScootsy; 
-(IBAction)pushJackiePass; 
-(IBAction)pushJackieIntro; 
-(IBAction)pushBB; 
-(IBAction)yourProblem; 
-(IBAction)pushSuckMy; 
-(IBAction)pushBallGirl; 
-(IBAction)pushGoofballs; 
-(IBAction)pushHavingFun; 
-(IBAction)pushwhoBambi; 
-(IBAction)pushExcited; 
-(IBAction)pushdirtyHippie; 
-(IBAction)pushBambiMother; 
-(IBAction)pushEverybodyPanic; 
-(IBAction)pushSmallChild; 
-(IBAction)pushBurnHouse; 
-(IBAction)pushTitanic; 
-(IBAction)pusherection; 

@end 

내 ViewController.m이다 : 나는 다양한 푸시 방법 (pushTropical, pushBurnHouse 등 내 'theAudio'변수를 해제하려고했습니다

#import "FirstViewController.h" 
#import "AVFoundation/AVAudioPlayer.h" 



@implementation FirstViewController 

-(IBAction)pushTropical { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"Tropical" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 





} 

-(IBAction)pushVakidis { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"Vakidis" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 
-(IBAction)pushBB { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"BB" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushTwiggy { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"Twiggy" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushScootsy { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"Scootsy" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushJackiePass { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"JackiePass" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushJackieIntro { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"JackieIntro" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushyourProblem { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"yourProblemShort" ofType:@"m4a"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushBallGirl { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"ballgirl" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushGoofballs { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"goofballs" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushSuckMy { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"yourfamily" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushHavingFun { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"dukesBase" ofType:@"m4a"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushwhoBambi { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"whoBambi" ofType:@"m4a"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushExcited { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"dukesIsExcited" ofType:@"m4a"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushdirtyHippie { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"dirtyhippie" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushBambiMother { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"BambiMother" ofType:@"m4a"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushEverybodyPanic { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"EverybodyPanic" ofType:@"m4a"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushSmallChild { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"SmallChild" ofType:@"m4a"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushBurnHouse { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"BurnHouse" ofType:@"m4a"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushTitanic { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"Titanic" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pusherection { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"erection" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

/* 
// The designated initializer. Override to perform setup that is required before the view is loaded. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 
*/ 

/* 
// Implement loadView to create a view hierarchy programmatically, without using a nib. 
- (void)loadView { 
} 
*/ 

/* 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 
*/ 

/* 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 


    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 

} 

@end 

.) like ... so

-(IBAction)pushExcited { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"dukesIsExcited" ofType:@"m4a"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 

    [theAudio release]; 





} 

... 그러나 소리가 전혀 들리지 않습니다. 그래서 나는 잃어 버렸습니다. 왜냐하면 내가 언제 어디서나 'theAudio'를 출시하려 할 때마다 그 기능을 릴리즈하려고 인식하지 못하기 때문입니다. 도움!

답변

3

클래스를 대리자로 설정 한 후에 audioPlayerDidFinishPlaying:successfully:AVAudioPlayerDelegate 메서드를 통해 AVAudioPlayer를 릴리스해야합니다. 이 작업을 완료하면

즉 :

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { 
    [player release]; 
} 

, 모두 잘해야한다.