Вопрос

Question is in the title: "Do I need to release SystemSoundID if I have ARC on?" Here is my Code:

NSURL *pathURL = [NSURL fileURLWithPath:path];
SystemSoundID soundid;
AudioServicesCreateSystemSoundID((__bridge_retained CFURLRef)pathURL, &soundid);
AudioServicesPlaySystemSound(soundid);

And if so, when do I release it? (I don't have a dealloc method because I'm using a static method and that cannot be changed)

Also, is this currently the best way to play a sound effect I heard this framework is now deprecated.

THANKS!

Это было полезно?

Решение

Yes, you do need to release it. ARC only concerns itself with Obj-C objects, and a SystemSoundID is not an obj-c object. At some point you do need to call AudioServicesDisposeSystemSoundID() on your SystemSoundID value. You could do this with a system sound completion routine (using AudioServicesAddSystemSoundCompletion()).

As for what sergio is talking about, you're leaking the pathURL object. You've used __bridge_retained, which transfers ownership of the object to CoreFoundation. You should probably just change that to (__bridge CFURLRef)pathURL, which will not transfer ownership. The AudioServices API will retain the object as necessary.

Другие советы

According to clang ref

(__bridge_retained T) op casts the operand, which must have retainable object pointer type, to the destination type, which must be a non-retainable pointer type. ARC retains the value, subject to the usual optimizations on local values, and the recipient is responsible for balancing that +1.

Thus, you need to do the release.

In other words, what happens in the bridge cast is that the ownership of pathURL is passe over to CF, and then out of ARC scope.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top