ARC内存管理问题与系统声音

时间:2012-05-06 16:12:36

标签: xcode memory automatic-ref-counting instruments

我正在使用...播放系统声音

    NSString *path = [NSString stringWithFormat:@"%@%@",
                      [[NSBundle mainBundle] resourcePath],
                      @"/heartbeat.wav"];

    //declare a system sound id
    SystemSoundID soundID4;

    //Get a URL for the sound file
     NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];

    //Use audio sevices to create the sound
    AudioServicesCreateSystemSoundID((__bridge_retained CFURLRef)filePath, &soundID4);

    //Use audio services to play the sound
    AudioServicesPlaySystemSound(soundID4);

    AudioServicesDisposeSystemSoundID(soundID4);

我不确定这是问题..但如果它以“分析”运行,则会出现潜在的泄漏。随着我在模拟器中运行更多次,应用变得越来越慢,显然会出现某种泄漏。我已经找到了如何在不使用arc的情况下处理这个问题的示例,但没有使用。任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:1)

“它出现了潜在的泄漏”。什么是“它”?

其次,您不会通过观察程序越来越慢来分析内存泄漏。实际上,这不是内存泄漏的正常症状,除非您耗尽物理RAM并导致大量交换。您可以通过使用泄漏工具等实际识别不再可访问的内存来诊断泄漏。

那就是说,你不想要__bridge_retained,你只需要__bridge。我更喜欢CFBridgingRetain()CFBridgingRelease()函数而不是__bridge_retained__bridge_transfer强制转换的原因之一是您不太可能犯这样的错误。例如,你从未写过:

AudioServicesCreateSystemSoundID(CFBridgingRetain(filePath), &soundID4);

首先,显而易见的是,不需要保留filePath只是为了将其传递给函数。其次,调用CFRetain() - 样式函数也可以清楚地表明您有责任调用CFRelease() - 样式函数来平衡它。

相关问题