使用 AVAudioPlayer 发生内存泄漏

发布于 2024-10-21 14:50:57 字数 2184 浏览 1 评论 0原文

我正在使用仪器分析 Objective-C++ 代码中的泄漏,并且得到以下信息:

Leaked Object   Address Size    Responsible Library Responsible Frame
Malloc 32 Bytes,0x8135bc0   32 Bytes    AudioToolbox    SimAggregateDevice::SimAggregateDevice(__CFString const*, __CFString const*, long&)
__NSCFDictionary,0x8135be0  48 Bytes    AudioToolbox    CreateDictionaryForDevice(unsigned long)
Malloc 32 Bytes,0x8135c10   32 Bytes    AudioToolbox    CreateDictionaryForDevice(unsigned long)
Malloc 32 Bytes,0x8135c30   32 Bytes    AudioToolbox    CreateDictionaryForDevice(unsigned long)
Malloc 48 Bytes,0x8135c50   48 Bytes    AudioToolbox    SimAggregateDevice::SimAggregateDevice(__CFString const*, __CFString const*, long&)
__NSCFDictionary,0x813a820  48 Bytes    AudioToolbox    SimAggregateDevice::SimAggregateDevice(__CFString const*, __CFString const*, long&)
__NSCFArray,0x813a850   32 Bytes    AudioToolbox    SimAggregateDevice::SimAggregateDevice(__CFString const*, __CFString const*, long&)
__NSCFDictionary,0x813a870  48 Bytes    AudioToolbox    CreateDictionaryForDevice(unsigned long)
Malloc 32 Bytes,0x813a8a0   32 Bytes    AudioToolbox    SimAggregateDevice::SimAggregateDevice(__CFString const*, __CFString const*, long&)
Malloc 32 Bytes,0x813a8d0   32 Bytes    AudioToolbox    CreateDictionaryForDevice(unsigned long)

我认为这与我编写的在应用程序启动时播放声音的代码有关。 applicationDidFinishLaunching:: 中调用此方法

- (void)playJingle {
    // This is a detached thread so I need a new pool.
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    // This audio player will be released in  audioPlayerDidFinishPlaying.
    NSString *newAudioFile = [[NSBundle mainBundle] pathForResource:@"jingle" ofType:@"m4a"];
    AVAudioPlayer *av = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile] error:NULL];
    [av setDelegate:self];
    [av play];

    // Release.
    [pool release];
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer*)av successfully:(BOOL)flag
{
    // Release the AVAudioPlayer.
    [av release];
}

我这样做:在后台线程中的 (使用 PerformSelectorInBackground:withObject: 调用)。我错过了什么吗?为什么我会有这些泄漏? 谢谢!

I'm analyzing leaks in my Objective-C++ code using the instrumentation, and I'm getting this:

Leaked Object   Address Size    Responsible Library Responsible Frame
Malloc 32 Bytes,0x8135bc0   32 Bytes    AudioToolbox    SimAggregateDevice::SimAggregateDevice(__CFString const*, __CFString const*, long&)
__NSCFDictionary,0x8135be0  48 Bytes    AudioToolbox    CreateDictionaryForDevice(unsigned long)
Malloc 32 Bytes,0x8135c10   32 Bytes    AudioToolbox    CreateDictionaryForDevice(unsigned long)
Malloc 32 Bytes,0x8135c30   32 Bytes    AudioToolbox    CreateDictionaryForDevice(unsigned long)
Malloc 48 Bytes,0x8135c50   48 Bytes    AudioToolbox    SimAggregateDevice::SimAggregateDevice(__CFString const*, __CFString const*, long&)
__NSCFDictionary,0x813a820  48 Bytes    AudioToolbox    SimAggregateDevice::SimAggregateDevice(__CFString const*, __CFString const*, long&)
__NSCFArray,0x813a850   32 Bytes    AudioToolbox    SimAggregateDevice::SimAggregateDevice(__CFString const*, __CFString const*, long&)
__NSCFDictionary,0x813a870  48 Bytes    AudioToolbox    CreateDictionaryForDevice(unsigned long)
Malloc 32 Bytes,0x813a8a0   32 Bytes    AudioToolbox    SimAggregateDevice::SimAggregateDevice(__CFString const*, __CFString const*, long&)
Malloc 32 Bytes,0x813a8d0   32 Bytes    AudioToolbox    CreateDictionaryForDevice(unsigned long)

I assume this is related to the code I wrote to play a sound when my application starts. I do it this way: this method is invoked in applicationDidFinishLaunching::

- (void)playJingle {
    // This is a detached thread so I need a new pool.
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    // This audio player will be released in  audioPlayerDidFinishPlaying.
    NSString *newAudioFile = [[NSBundle mainBundle] pathForResource:@"jingle" ofType:@"m4a"];
    AVAudioPlayer *av = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile] error:NULL];
    [av setDelegate:self];
    [av play];

    // Release.
    [pool release];
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer*)av successfully:(BOOL)flag
{
    // Release the AVAudioPlayer.
    [av release];
}

in a background thread (invoked with performSelectorInBackground:withObject:). Am I missing something? Why do I have those leaks?
Thanks!

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

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

发布评论

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

评论(2

一袭水袖舞倾城 2024-10-28 14:50:58

您是否尝试过在设备上运行该应用程序? AVAudioPlayer 在模拟器上进行内存泄漏检测时很容易出现误报...

这是一个非常相似的问题,带有相似的泄漏报告

Have you tried running the application on the device? AVAudioPlayer is prone to false positives with memory leak detection on the simulator...

Here's a very similar question with a similar looking leak report

早乙女 2024-10-28 14:50:58

这是行不通的,您将 AVPlayer 分配给局部变量:

AVAudioPlayer *av = ...

而不是实例变量。

-(void>) playJinlge{
    ...
    self.av = .....
}

-(void) didfinish ... {
   [self.av release];
   self.av=nil;
}

This can't work, you assign the AVPlayer to a local variable:

AVAudioPlayer *av = ...

and not to an instance variable.

-(void>) playJinlge{
    ...
    self.av = .....
}

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