Memory leak in AudioServicesPlaySystemSound using ARC

automatic-ref-counting, memory-leaks, objective-c

Solution

Replace `__bridge_retained` by `__bridge`.

`__bridge_retained` would mean that you transfer the ownership of `aFileURL` to `AudioServicesCreateSystemSoundID()` and that function would have to release it (which it doesn't).

And I think you should also call

AudioServicesDisposeSystemSoundID(myID)

when the sound object is no longer needed.

Tip: When the static analyzer shows the "Potential leak" warning, click on the blue icon to the left of the warning and you will see detailed information about the problem.

Problem

I would need some help solving this memory leak problem. I am using ARC. The potential leak is on this line: ``` NSURL *aFileURL = [NSURL fileURLWithPath:filePath isDirectory:NO]; ``` Here is the code: ``` // === 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); } ```

Original source