以下情况出现内存泄漏问题

发布于 2024-11-03 17:11:25 字数 996 浏览 0 评论 0原文

当我单击播放按钮时,我遇到内存泄漏...

我正在使用“运行和性能工具”下的“泄漏”工具进行测试...在模拟器上,

当我首先单击播放按钮时,我遇到内存泄漏时间......

这是我的代码......

-(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆光下的微笑 2024-11-10 17:11:25

我认为,下面的陈述是内存泄漏的根源,

player = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:&err];

这是讨论相同问题的SO帖子。

AVAudioPlayer内存泄漏

AVAudioPlayer 内存泄漏

AVAudioPlayer 内存泄漏 - 媒体播放器框架< /a>

这是博客文章

AVAudioPlayer 内存泄漏

编辑:

根据博客教程,您的代码必须如下所示。

-(void)setPlayer
{

    NSURL *file = [[NSURL alloc] initFileURLWithPath:
                   [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"shut up.mp3"]];
    NSError *err = nil;

    NSData *data = [NSData dataWithContentsOfFile:file];
    AVAudioPlayer *player = [AVAudioPlayer alloc];    
    if([player initWithData:audioData error:NULL])
     {

        player.numberOfLoops = -1;
        [player prepareToPlay];
        player.volume=1.0;
        [player autorelease];
    } 
    else 
    {
        [player release];
        player = nil;
    }
    [file release];
}

无泄漏版本存储 alloc 返回的指针,而不是 initWithData:error: 返回的指针。这样,无论发生什么,玩家仍然可以被释放。

I think, the below statement is the source of memory leak,

player = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:&err];

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.

-(void)setPlayer
{

    NSURL *file = [[NSURL alloc] initFileURLWithPath:
                   [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"shut up.mp3"]];
    NSError *err = nil;

    NSData *data = [NSData dataWithContentsOfFile:file];
    AVAudioPlayer *player = [AVAudioPlayer alloc];    
    if([player initWithData:audioData error:NULL])
     {

        player.numberOfLoops = -1;
        [player prepareToPlay];
        player.volume=1.0;
        [player autorelease];
    } 
    else 
    {
        [player release];
        player = nil;
    }
    [file release];
}

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.

谷夏 2024-11-10 17:11:25

Jhaliya 的回答中的博客文章描述了一个泄漏,该泄漏是特定于播放器无法初始化音频(例如找不到文件)的情况。

您的代码的真正问题是,只有当用户明确停止音频时,您才释放播放器。如果音频播放到最后,您就有一个保留计数为 1 的播放器实例。然后,如果用户再次点击播放,您将创建一个新播放器并将其分配给 player 变量,从而泄漏老的。

最简单的解决方案是使 player 成为保留属性:

@property(nonatomic,retain)AVAudioPlayer *player;

然后,不要直接分配给 ivar,而是使用 mutator 来设置播放器,这将隐式释放先前设置的实例(如果存在)一:

[self setPlayer:[[[AVAudioPlayer alloc] initWithContentsOfURL:file error:&err] autorelease];

不要忘记在你的 dealloc 中释放它:

-(void)dealloc {
    [player release];
    [super 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:

@property(nonatomic,retain)AVAudioPlayer *player;

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:

[self setPlayer:[[[AVAudioPlayer alloc] initWithContentsOfURL:file error:&err] autorelease];

And don't forget to release it in your dealloc:

-(void)dealloc {
    [player release];
    [super dealloc];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文