Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with pointer conversion using Automatic Reference Counting (ARC)

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?

like image 513
Chetan Avatar asked Jul 13 '11 19:07

Chetan


People also ask

What is automatic reference counting in Swift explain how it works?

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.

Does Objective-C support Arc?

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.

What is __ bridge?

__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 would you explain arc to a new IOS developer?

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.


1 Answers

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;
like image 52
Tommy Avatar answered Oct 21 '22 14:10

Tommy