我想在我的應用程序 發揮每按一下按鈕點擊聲音,當我創建的,其h和.M如下內存泄漏維持性
.h文件中的實用程序類
@interface SoundPlayUtil : NSObject<AVAudioPlayerDelegate,AVAudioSessionDelegate>
{
AVAudioPlayer *audioplayer;
}
@property (retain, nonatomic) AVAudioPlayer *audioplayer;
-(id)initWithDefaultClickSoundName;
-(void)playIfSoundisEnabled;
@end
.m文件
@implementation SoundPlayUtil
@synthesize audioplayer;
-(id)initWithDefaultClickSoundName
{
self = [super init];
if (self)
{
NSString* BS_path_blue=[[NSBundle mainBundle]pathForResource:@"click" ofType:@"mp3"];
self.audioplayer =[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:BS_path_blue] error:NULL];
[self.audioplayer prepareToPlay];
}
return self;
}
-(void)playIfSoundisEnabled
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:soundStatus]==YES)
{
[self.audioplayer play];
}
}
-(void)dealloc
{
[audioplayer release];
[super dealloc];
}
@end
,並在任何類別按鈕點擊我做
SoundPlayUtil *obj = [[SoundPlayUtil alloc] initWithDefaultClickSoundName];
[obj playIfSoundisEnabled];
[obj release];
它的工作正常,我成功播放聲音。當我分析代碼時出現問題。 編譯器顯示內存泄漏initWithDefaultClickSoundName實用程序類的.m中的方法,因爲我將alloc方法發送到self.audioplayer並且未釋放它。
發佈此對象的最佳地點是什麼?
是否使用ARC? –
不,沒有使用ARC –