使用ARC在AudioServicesPlaySystemSound中发生内存泄漏

Pet*_*erK 2 memory-leaks objective-c automatic-ref-counting

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

潜在的泄漏就在这条线上:

NSURL *aFileURL = [NSURL fileURLWithPath:filePath isDirectory:NO];
Run Code Online (Sandbox Code Playgroud)

这是代码:

// === 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);
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*n R 9

替换__bridge_retained__bridge.

__bridge_retained岂不是转移所有权aFileURL,以AudioServicesCreateSystemSoundID()和该功能将不得不释放它(它没有).

我想你也应该打电话

AudioServicesDisposeSystemSoundID(myID)
Run Code Online (Sandbox Code Playgroud)

当不再需要声音对象时.

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