My project is using Automatic Reference Counting, and I'm trying to use the following Accessibility API function:
extern AXError AXUIElementCopyAttributeValue (
AXUIElementRef element,
CFStringRef attribute,
CFTypeRef *value);
To call the function, I'm doing something like this:
NSArray *subElements = nil;
AXUIElementCopyAttributeValue(..., (CFArrayRef *)&subElements);
However, ARC is throwing the following error regarding the last argument:
error: Automatic Reference Counting Issue: Cast of an indirect pointer to an Objective-C pointer to 'CFArrayRef *' (aka 'const struct __CFArray **') is disallowed with ARC
How do I resolve this?
Automatic Reference Counting (ARC) is a memory management attribute used to monitor and manage an application's memory usage. Swift memory management works automatically without control. It automatically allocates or de-allocates memory to allow efficient running of applications.
Automatic Reference Counting (ARC) is a memory management option for Objective-C provided by the Clang compiler. When compiling Objective-C code with ARC enabled, the compiler will effectively retain, release, or autorelease where appropriate to ensure the object's lifetime extends through, at least, its last use.
__bridge transfers a pointer between Objective-C and Core Foundation with no transfer of ownership. __bridge_retained or CFBridgingRetain casts an Objective-C pointer to a Core Foundation pointer and also transfers ownership to you.
How ARC Works. Every time you create a new instance of a class, ARC allocates a chunk of memory to store information about that instance. This memory holds information about the type of the instance, together with the values of any stored properties associated with that instance.
Have you tried using an intermediate CFArrayRef, so that you can still pass a pointer to a ref (ie, a pointer to a pointer) to AXUIElementCopyAttributeValue
, but can then achieve the toll-free bridge with just an ordinary cast? E.g.
CFArrayRef subElementsCFArray;
AXUIElementCopyAttributeValue(..., &subElementsCFArray);
NSArray *subElements = (__bridge NSArray *)subElementsCFArray;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With