Re: "Potential" Leak of an Object Allocated...
Re: "Potential" Leak of an Object Allocated...
- Subject: Re: "Potential" Leak of an Object Allocated...
- From: Steve Sisak <email@hidden>
- Date: Mon, 17 Sep 2012 20:54:40 -0400
At 6:21 PM -0600 9/17/12, Philip McIntosh wrote:
Here is the statement and the potential problem reported after Analyzing:
else
{
NSURL *buttonSound =
[[NSBundle mainBundle] URLForResource: @"MachineClick"
withExtension: @"wav"];
self.buttonSoundFileURLRef =
(__bridge_retained CFURLRef) buttonSound;
AudioServicesCreateSystemSoundID (buttonSoundFileURLRef_,&buttonSoundFileObject_);
}
Potential leak of an object allocated on line 791 and stored into
'buttonSound'
Method returns an Objective-C object with a +0 retain count
Reference count incremented. The object now has a +1 retain count
Object leaked: object allocated and stored into 'buttonSound' is not
referenced later in this execution path and has a retain count of +1
(Sorry, I'm typing this in Eudora, not Xcode)
I think you may be prematurely optimizing here.
I generally use stack-based locals for all of the CF calls and then
update the Obj-C properties if everything succeeds -- this way your
object is always left in a consistent state, even on failure.
Rather than passing the address of your instance variable to
AudioServicesCreateSystemSoundID (), use a local variable, set the
property from the local variable (retaining it) and CFRelease the
local.
This way the retains/releases in the local match and Obj-C manages
the retain for the property.
Think about what would happen if buttonSoundFileObject_ were already
a valid object and AudioServicesCreateSystemSoundID() failed for some
reason.
While this generates an extra retain/release, you're only ever doing
this once so the performance impact is negligible.
HTH,
-Steve
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden