iPhone 编程:avaudioplayer 在播放时泄漏内存

发布于 2024-08-05 16:19:24 字数 1160 浏览 3 评论 0原文

我是使用 avadioplayer 的新手,每次播放声音时我似乎都有记忆。 我无法弄清楚我缺少什么来摆脱仪器内的它。这可能是误报吗?

ViewController.h :

@interface ISpectatorViewController : UIViewController <UIAccelerometerDelegate>{

AVAudioPlayer *massCheerSoundID;

}

@property(nonatomic,retain) AVAudioPlayer * massCheerSoundID;

// ViewController.m

- (void)viewDidLoad {

    NSString * filePath;

    filePath = [[NSBundle mainBundle] pathForResource:@"massCheer" ofType:@"mp3"];

    massCheerSoundID = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filePath ]error:nil];

}

- (void) playSound
{

        if(massCheerSoundID.playing == false)
    {

        massCheerSoundID.currentTime = 0.0;


                //leak here
        [massCheerSoundID play];
    }
}

- (void)dealloc {

    [super dealloc];
    [massCheerSoundID release];

}

我发现问题是什么了。

我必须在界面上添加 AVAudioPlayerDelegate ,因为我已经设置了 UIAccelerometerDelegate

@interface iSpectatorViewController: UIViewController<AVAudioPlayerDelegate>

并设置了

massCheerSoundId.delegate = self

I'm new to using avadioplayer and I seems to have a memory when ever I play a sound.
I cannot figure out what I am missing to get rid of it inside Instrument. could this be a false positive?

ViewController.h :

@interface ISpectatorViewController : UIViewController <UIAccelerometerDelegate>{

AVAudioPlayer *massCheerSoundID;

}

@property(nonatomic,retain) AVAudioPlayer * massCheerSoundID;

// ViewController.m

- (void)viewDidLoad {

    NSString * filePath;

    filePath = [[NSBundle mainBundle] pathForResource:@"massCheer" ofType:@"mp3"];

    massCheerSoundID = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filePath ]error:nil];

}

- (void) playSound
{

        if(massCheerSoundID.playing == false)
    {

        massCheerSoundID.currentTime = 0.0;


                //leak here
        [massCheerSoundID play];
    }
}

- (void)dealloc {

    [super dealloc];
    [massCheerSoundID release];

}

I found out what the problem is.

I for got to add the AVAudioPlayerDelegate on the interface since I've set the UIAccelerometerDelegate instead

@interface iSpectatorViewController: UIViewController<AVAudioPlayerDelegate>

and set the

massCheerSoundId.delegate = self

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

尝试将 MediaPlayer.framework 添加到您的项目中,无需更改代码

Try adding MediaPlayer.framework to your project, with no code changes

空心空情空意 2024-08-12 16:19:24

今天发现同样的泄漏。添加 MediaPlayer.framework 似乎可以解决这个问题。

Found the same leak today. Adding MediaPlayer.framework seems to fix that.

ゞ记忆︶ㄣ 2024-08-12 16:19:24

这可能是你的dealloc方法吗?

我认为惯例是首先release,然后调用[super dealloc]

我一直认为这意味着“首先发布你的东西,然后让你的父类处理它”。

您的 dealloc 方法应为:

- (void)dealloc {

    [massCheerSoundID release];
    [super dealloc];

}

Could it be your dealloc method?

I think the convention is to first release and then call [super dealloc].

I always assumed this was meant to "first release your stuff, then have your parent class handle it".

Your dealloc method should read:

- (void)dealloc {

    [massCheerSoundID release];
    [super dealloc];

}
奢华的一滴泪 2024-08-12 16:19:24

今天发现同样的泄漏。添加 MediaPlayer.framework 似乎可以解决这个问题。

这对我也有用。奇怪的。

Found the same leak today. Adding MediaPlayer.framework seems to fix that.

That worked for me also. Strange.

羁拥 2024-08-12 16:19:24

问题可能是,如果您不释放视图,则您会一遍又一遍地重新启动massCheerSoundId。因此,如果您要添加子视图,则 viewDidLoad 会被调用并初始化播放器,那么您可能要删除子视图并再次添加它,因为您没有释放视图(假设您只有一份副本)dealloc 不会被调用(无论如何我不这么认为),那么如果您重新添加视图massCheerSoundId将被覆盖并泄漏..,另一个问题可能是,由于您将massCheerSoundId作为属性,您分配它并且生成的setter也保留它,所以您当它应该只+1时留下+2引用计数...所以假设我的假设是正确的我建议像这样编写viewDidLoad

    - (void)viewDidLoad {    
    NSString * filePath;   
     filePath = [[NSBundle mainBundle] pathForResource:@"massCheer" ofType:@"mp3"]; 
    if(massCharSoundId!=nil)
{
       AVAudioPlayer *p= [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURLfileURLWithPath:filePath ]error:nil];}
       massCheerSoundID = p;
     [p release];
}

Problem might be that you are reinitating the massCheerSoundId over and over if you arent r eleasing your view. So, if you are adding your subview, viewDidLoad gets called and you initialize your player, then perhaps you are removing the subview and adding it again, since you didnt release the view (assuming you have only one copy) dealloc does not get called (i dont think so anyway ), then if you re add the view massCheerSoundId will get overriden and leak.., another problem might be that since you have massCheerSoundId as a property, you allocate it and the setter generated also retains it, so then you are left with a +2 reference count when it should only be +1...so assuming my assumptions are correct i would suggest to write viewDidLoad like this

    - (void)viewDidLoad {    
    NSString * filePath;   
     filePath = [[NSBundle mainBundle] pathForResource:@"massCheer" ofType:@"mp3"]; 
    if(massCharSoundId!=nil)
{
       AVAudioPlayer *p= [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURLfileURLWithPath:filePath ]error:nil];}
       massCheerSoundID = p;
     [p release];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文