![](/img/trans.png)
[英]iPhone Memory Leak from AudioToolbox when using MPMoviePlayerViewController iOS 4.2
[英]AudioToolBox leak in iOS6?
當我使用AudioToolBox播放音樂時,內存大量泄漏。
AVAudioPlayer *newMusicPlayer = [[AVAudioPlayer alloc] initWithData:data error:&error];
我使用此代碼播放音樂。 在iOS5和iOS4中,它可以正常運行。 但是在iOS6中,如果數據大小為5M,則所有5M都會泄漏。 而且我在儀器中看不到泄漏信息。
有人有同樣的問題嗎? 任何建議將不勝感激。
我在這里的所有音頻代碼(使用ARC):
@implementation ViewController
{
AVAudioPlayer *_player;
}
- (void)play
{
if (_player)
{
[_player stop];
_player = nil;
}
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"test.app/1.mp3"];
NSData *musicData = [[NSData alloc] initWithContentsOfFile:path];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:musicData error:nil];
player.volume = 1;
if (player)
{
_player = player;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 100, 100, 100);
[button setTitle:@"play" forState:UIControlStateNormal];
[button addTarget:self action:@selector(play) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
@end
我正在使用AVAudioPlayer
播放文件中的音頻,發現iOS模擬器始終會泄漏內存,但設備不會。 使用儀器已對此進行了驗證。 這是ARC代碼。
我的播放器的聲明如下:
@property (nonatomic, retain) AVAudioPlayer *numberPlayer;
並像這樣合成:
@synthesize numberPlayer = _numberPlayer;
由於我需要播放幾種不同的聲音,並且AVAudioPlayer
在創建后無法重置以播放不同的音頻文件,因此每次都會創建一個新的播放器,如下所示:
NSString *audioFilePathName = [NSString stringWithFormat:@"Voices/%@/%03i.m4a", self.voiceName, self.theNumber];
NSURL *url = [NSURL fileURLWithPath:BUNDLE_FULL_PATH(audioFilePathName)];
self.numberPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
self.numberPlayer.numberOfLoops = 0;
[self.numberPlayer setCurrentTime:0.0];
[self.numberPlayer setDelegate:self];
[self.numberPlayer prepareToPlay];
[self.numberPlayer play];
在我的代表中,我將播放器播放完畢后將其設置為nil
:
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
if (player == self.numberPlayer) {
_numberPlayer = nil;
}
}
在模擬器中, AudioSessionDevice
和UISoundNewDevice
泄漏內存。 但是,最大的泄漏者實際上是NSURL
。 這些泄漏均不會在設備上發生。 此行為在iOS 6中沒有更改,但是如果有任何區別,我確實將項目部署設置為5.0。
另請參閱https://stackoverflow.com/questions/3433487/avaudioplayer-leak-in-simulator和AudioToolbox庫AVAudioPlayer中的內存泄漏 。
也許您比我做得更好,而且我還沒有XCode,但是:
已創建對象,已創建引用:
AVAudioPlayer *newMusicPlayer = [[AVAudioPlayer alloc] initWithData:data error:&error];
它是分配而不是分配,不保留:
_musicPlayer = newMusicPlayer;
如果不使用playMusicWithMusicData
函數,它將取消分配newMusicPlayer
並且當我使用_musicPlayer
時,它將使我在其他函數中的代碼崩潰。 分析器工具如此之多,以至於我做了保留,也許還發布了。
如果您使用的是ios4,則不是該項目。
在if分支中:
[_musicPlayer stop];
_musicPlayer = nil;
我會檢查_musicPlayer
是否為nil,但這不會解決您的問題。
不知道它可能在哪里,如果不是在ios中,可能在那里:
確實解決了您是否更改委托而不是自我的問題。
_musicPlayer.delegate = self;
上其他班嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.