Converting Project To ARC With Double Indirection Pointer Assign

1.7k Views Asked by At

I'm trying to convert a project to ARC. The project has a Directed Acyclic Word Graph, which basically means lots of double indirection pointers everywhere.

This is proving quite a challenge to convert to ARC, and one issue in particular has me currently stumped.

Here's the scenario.

Let's say you have an NSString *:

NSString *b = [[NSString alloc] initWithString:@"hello"];

You also have a double indirection type:

__unsafe_unretained NSString **a;

You want to assign one to the other as follows:

    a = &b;

This gives a conversion error:

error: assigning 'NSString *__strong *' to 'NSString *__unsafe_unretained *' changes retain/release properties of pointer

Changing b to __unsafe_unretained doesn't work. I've also tried the various bridging casts. Am I missing something obvious here?

Any ideas?

Thanks!

1

There are 1 best solutions below

5
On BEST ANSWER

You can use a pointer-to-const-pointer to avoid memory management issues:

__attribute__((objc_precise_lifetime)) NSString *b = [[NSString alloc] initWithString:@"hello"];
NSString *const*a;
a = &b;

you need to use objc_precise_lifetime to make b available for the whole context (ARC may release b after last reference)

EDIT: This can also be used (but be aware of managing your double pointer)

NSString *b = [[NSString alloc] initWithString:@"hello"];
NSString *__strong*a;
a = &b;