-(IBAction)play2;
{
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef =CFBundleCopyResourceURL(mainBundle,
(CFStringRef) @"Bear3", CFSTR ("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}
This is giving me an error:
potential leak of an object allocated " CFBundleResourceURL
returns a Core Foundation object with a +1 retain count
CFBundleCopyResourceURL
creates aCFURLRef
object that you own, so you need to relinquish ownership of this object at some point withCFRelease
. Similarly you will need to balance your call toAudioServicesCreateSystemSoundID
with another call toAudioServicesDisposeSystemSoundID
.For Core Foundation, functions that have the word
Create
orCopy
in their name return an object that you own, so you must relinquish ownership of it when you are done with it. For more information about Core Foundation memory management, see the Core Foundation Memory Management Programming Guide.Just to give you a hint, I would probably handle the memory management like this (although I haven't coded Objective-C for a while). This also assumes you want to keep the URL reference for whatever reason:
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID); - leak here, because create added to retain count
use AudioServicesDisposeSystemSoundID after playing sound
CFBundleCopyResourceURL
contains copy so your retain count onsoundFileURLRef
is in fact 1. When you are done with it callCFRelease(soundFileURLRef)
to decrement your retain count.In addition to the error you're getting, SAKrisT's answer about calling
AudioServicesDisposeSystemSoundID
on the object you created withAudioServicesCreateSystemSoundID
is also something to address.If you're not using ARC (available in xcode 4.2) then you need to release anything you alloc. add
[alert release]
after[alert show]
.Whenever you use the keyword 'alloc' , it means you are allocating some memory space for your object. Now if you don't release it yourself or autorelease it, then it shows 'memory leak'. It is not only about uialertview, but for every other objects also.
You may want to release the alertview object in dealloc() method, but still it will show memory leak as the memory is unused for a long time.
So , first you show the alert by [alert show], then you need the object anymore, so release it by [alert release];
Enjoy !! :)