【问题标题】:Memory leak in AudioServicesPlaySystemSound using ARC使用 ARC 的 AudioServicesPlaySystemSound 中的内存泄漏
【发布时间】:2012-08-12 08:33:11
【问题描述】:

我需要一些帮助来解决这个内存泄漏问题。我正在使用 ARC。

潜在的泄漏在这一行:

NSURL *aFileURL = [NSURL fileURLWithPath:filePath isDirectory:NO];

代码如下:

// === Check if the game should play sound === //
if (sound == YES) {
    //==== PLAY THE LOOSING SOUND ====//
    // Form a URL to the sound file, which in init using the Path
    NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *filePath = [mainBundle pathForResource:@"wrong2" ofType:@"wav"];
    NSURL *aFileURL = [NSURL fileURLWithPath:filePath isDirectory:NO];

    // Create a sound ID, 
    SystemSoundID myID;
    // Register the sound
    AudioServicesCreateSystemSoundID((__bridge_retained CFURLRef)aFileURL, &myID) ;
    // Play the sound!
    AudioServicesPlaySystemSound(myID);
}

【问题讨论】:

    标签: objective-c memory-leaks automatic-ref-counting


    【解决方案1】:

    __bridge_retained 替换为__bridge

    __bridge_retained 意味着您将aFileURL 的所有权转让给AudioServicesCreateSystemSoundID(),并且该函数必须释放它(它不会)。

    我认为你也应该打电话

    AudioServicesDisposeSystemSoundID(myID)
    

    当不再需要声音对象时。

    提示:当静态分析仪显示“潜在泄漏”警告时,单击警告左侧的蓝色图标,您将看到有关问题的详细信息。

    【讨论】:

      猜你喜欢
      • 2012-09-14
      • 2012-02-20
      • 1970-01-01
      • 2012-04-03
      • 1970-01-01
      • 1970-01-01
      • 2013-05-22
      • 2012-10-05
      • 1970-01-01
      相关资源
      最近更新 更多