Convert a project to ARC with a double direction indicator

I am trying to convert a project to ARC. The project has a directional acyclic word diagram which basically means a lot of double pointer pointers.

This is a pretty daunting task to convert to ARC, and one problem in particular is currently deadlocked.

Here's the script.

Let's say you have NSString *

:

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

      

You also have a double type of indirection:

__unsafe_unretained NSString **a;

      

You want to assign one of them like this:

    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 have also tried various bridges. Am I missing something obvious here?

Any ideas?

Thank!

+3


source to share


1 answer


You can use pointer-pointer-pointer to avoid memory management problems:

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

      

you need to use objc_precise_lifetime to make the b

whole context available (ARC can free b

after the last link)



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

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

      

+5


source







All Articles