On Nov 11, 2010, at 14:21, Bill Cheeseman wrote: On Nov 11, 2010, at 7:04 AM, Peter Lübke wrote: - (id)valueForAttributeCopy:(CFStringRef)attribute { id value = nil; id axValue = nil; NSArray *attrNames;
if (AXUIElementCopyAttributeNames((CFTypeRef)_element, (CFArrayRef *)&attrNames) == kAXErrorSuccess) { if ( [attrNames indexOfObject:(NSString *)attribute] != NSNotFound && AXUIElementCopyAttributeValue((CFTypeRef)_element, attribute, (CFTypeRef *)&axValue) == kAXErrorSuccess ) value = axValue; } [attrNames release];
return value; }
I believe it's OK to cast a CFTypeRef to a toll-free-bridged NSObject and then 'release' or 'autorelease' it instead of 'CFReleas'ing it.
However, you have neglected to CFRelease (or release, or autorelease) the value stored in axValue by the AXUIElementCopyAttributeValue() function call in the body of your method The Cocoa convention is that a method like your -valueForAttributeCopy: always returns an object autoreleased. So, you might want to add '[axValue autorelease]' or '[value autorelease]' before the return, or change the last line to 'return [value autorelease]', and see what happens. (If the caller of your method does releases its return value when done with it, remove that release because it would be automatically released by the autorelease added in your method).
Actually, no. The docs (Memory Management Programming Guide) say that the returned value in this case should be retained:
"You “create” an object using a method whose name begins with “alloc” or “new” or contains “copy” "
Note the word "contains", which only applies to "copy".
(I personally think this rule is wrong, or at least formulated wrongly and inconsistently in the docs. For instance compare a method initWithObjects:copyItems: which clearly *contains* the word "copy" but whose return value isn't explicitly retained. But Apple does not agree with me, and they refuse to be unambiguous in the docs, even though it's pretty straightforward to be clear.)
Apart from this, I would advice to just avoid any confusion, and rename the method: either name it -valueForAttribute: and autorelease the value, or name it -copyValueForAttribute:.
Christiaan
|