以下情况出现内存泄漏问题
当我单击播放按钮时,我遇到内存泄漏...
我正在使用“运行和性能工具”下的“泄漏”工具进行测试...在模拟器上,
当我首先单击播放按钮时,我遇到内存泄漏时间......
这是我的代码......
-(IBAction)play
{
[self setPlayer];
[self playme];
}
-(IBAction)stop
{
[self stopme];
[self releasePlayer];
}
-(void)setPlayer
{
NSURL *file = [[NSURL alloc] initFileURLWithPath:
[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"shut up.mp3"]];
NSError *err = nil;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:&err];
[file release];
player.numberOfLoops = -1;
[player prepareToPlay];
player.volume=1.0;
}
-(void)playme
{
if (!isPlaying)
{
[player play];
isPlaying=YES;
}
}
-(void)stopme
{
if (isPlaying)
{
[player stop];
isPlaying=NO;
}
}
-(void)releasePlayer
{
if(!isPlaying)
{
[player release];
player=nil;
}
isPlaying=NO;
}
I am getting a memory leak when i click the play button....
I am testing with that "Leak" tool under "Run and performance tool"....on simulator
I am getting that leak when i click the play button first time.....
Here is my code....
-(IBAction)play
{
[self setPlayer];
[self playme];
}
-(IBAction)stop
{
[self stopme];
[self releasePlayer];
}
-(void)setPlayer
{
NSURL *file = [[NSURL alloc] initFileURLWithPath:
[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"shut up.mp3"]];
NSError *err = nil;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:&err];
[file release];
player.numberOfLoops = -1;
[player prepareToPlay];
player.volume=1.0;
}
-(void)playme
{
if (!isPlaying)
{
[player play];
isPlaying=YES;
}
}
-(void)stopme
{
if (isPlaying)
{
[player stop];
isPlaying=NO;
}
}
-(void)releasePlayer
{
if(!isPlaying)
{
[player release];
player=nil;
}
isPlaying=NO;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为,下面的陈述是内存泄漏的根源,
这是讨论相同问题的SO帖子。
AVAudioPlayer内存泄漏
AVAudioPlayer 内存泄漏
AVAudioPlayer 内存泄漏 - 媒体播放器框架< /a>
这是博客文章
AVAudioPlayer 内存泄漏
编辑:
根据博客教程,您的代码必须如下所示。
无泄漏版本存储 alloc 返回的指针,而不是 initWithData:error: 返回的指针。这样,无论发生什么,玩家仍然可以被释放。
I think, the below statement is the source of memory leak,
Here is the SO posts which has discussed the same issue.
AVAudioPlayer memory leak
AVAudioPlayer memory leak
AVAudioPlayer Memory Leak - Media Player Framework
Here is the blog post
AVAudioPlayer Memory Leak
EDITED:
As per the blog tutorial your code must be look like below.
The leak-free version stores the pointer returned by alloc, rather than the pointer returned by initWithData:error:. That way, whatever happens, the player can still be released.
Jhaliya 的回答中的博客文章描述了一个泄漏,该泄漏是特定于播放器无法初始化音频(例如找不到文件)的情况。
您的代码的真正问题是,只有当用户明确停止音频时,您才释放播放器。如果音频播放到最后,您就有一个保留计数为 1 的播放器实例。然后,如果用户再次点击播放,您将创建一个新播放器并将其分配给
player
变量,从而泄漏老的。最简单的解决方案是使
player
成为保留属性:然后,不要直接分配给 ivar,而是使用 mutator 来设置播放器,这将隐式释放先前设置的实例(如果存在)一:
不要忘记在你的 dealloc 中释放它:
The blog post in Jhaliya's answer describes a leak that's specific to the situation when your player can't init the audio, for example when it can't find the file.
The real problem with your code is that you only release the player if the user explicitly stops the audio. If the audio plays through to the end, you have a player instance with a retainCount of 1. Then if the user hits play again, you create a new player and assign it to the
player
variable, leaking the old one.The easiest solution to this is to make
player
a retained property:Then, instead of assigning to the ivar directly, use the mutator to set the player, which will implicitly release the previously set instance, if there is one:
And don't forget to release it in your dealloc: