PDA

View Full Version : Fixing Potential leaks found in CLANG




midntdj
Oct 29, 2009, 03:48 PM
I just started to use CLANG to help find any leaks with in my code. I have a some code that CLANG shows a potential leak, but I'm not sure what I need to do to resolve it. Any help would be greatly appreciated (assuming there is something wrong with how I implemented the audio).

I'm not sure that this is truly a leak or just a warning that CLANG doesn't like. In either case, I want to clean up my code so there are no warnings or potential leaks.

Here is the code that has the potential leak (baseURL is what CLANG is complaining about):


id sndpath = [[NSBundle mainBundle] pathForResource:@"Select" ofType:@"caf" inDirectory:@"/"];
CFURLRef baseURL = (CFURLRef)[[NSURL alloc] initFileURLWithPath:sndpath];

AudioServicesCreateSystemSoundID (baseURL, &btnTouchID);



This is called in my viewDidLoad routine to set up the sound when a button is touched.

In te dealloc routine I do dispose of the btnTouchID.

Is there a better way to define your SoundID?

Thanks.



kainjow
Oct 29, 2009, 07:20 PM
You're creating baseURL but not releasing it in that method, which is why Clang is complaining. Either release it manually, autorelease it, or use the autorelease convenience method instead:

CFURLRef baseURL = (CFURLRef)[NSURL fileURLWithPath:sndpath];

midntdj
Oct 30, 2009, 01:55 PM
use the autorelease convenience method instead:

CFURLRef baseURL = (CFURLRef)[NSURL fileURLWithPath:sndpath];

Thanks kainjow for your help. The autorelease convenience method you listed, corrected the CLANG warning. I had tried earlier to manually releasing the method, but CLANG still gave me the warning.

By changing the intifileURLWithPath to use fileURLWithPath seems to be the better solution.

Thank you again.