Objective-C ARC __weak variable reference triggers warning "The __weak attribute cannot be specified in an automatic variable"

I have an application (ios5 only) that inside the method declares a weak variable used inside the block to refer to the view controller instance.

QRCodeViewController * __weak weakSelf = self; 

The problem is that the compiler shows a warning:

__ weak attribute cannot be specified in an automatic variable

In this application, I used a lot of weak links, and I never saw such a warning, the only difference from other classes is that this class is implemented in a .mm file, since it uses a C ++ object, and the project can't compile if I leave it as .m.
I have to say that the code is working fine.
Any suggestion?

+6
source share
2 answers

Based on the same warning, I make it disappear using the __block attribute as follows:

 __block __weak MyViewController* weakSelf = self; 
+4
source

I am not sure why the warning states that __weak cannot be specified. AFAIK should be specified even if it is very dangerous to use __weak for automatic variables.

Apple's documentation says that the compiler will provide a warning if you do, but the actual text of the warning seems to imply that the attribute will be ignored not only because its use is dangerous, while the documentation seems to mean it's dangerous but it works as expected (if you really understand what to expect).

You will need to do some experimentation to check if the warning text is inappropriate, or if the documentation has not been updated correctly.

0
source

Source: https://habr.com/ru/post/923543/


All Articles