3

I have encountered the following error while compiling an Objective-C class:

VideoView.h:7: error: __block attribute can be specified on variables only

Also here is the important part of the header file:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface VideoView :UIView{
@private
    __block AVPlayer *player;   
}
...

Is there any explanation why g++ thinks that I'm applying the __block attribute on a non-variable object ?

2
  • 2
    can you show your complete .h file, i have tried the same and no error for me. Commented May 12, 2012 at 16:29
  • 1
    If it didn't toss an error, it was likely because of an old compiler. Commented May 12, 2012 at 18:23

1 Answer 1

13

You can't have __block on an instance variable as it is entirely unnecessary.

Namely, when you do:

^{
     someIvar = ....;
 }();

The block is capturing an immutable, retained, reference to self and referring to the iVar indirectly through that and, thus, __block does nothing as the variable is neither const-copied nor readonly.

Incidentally, this is also why, under ARC, you can end up with a "circular reference" warnings when using an iVar.

Note: We tried to think of a syntax for denoting this bit of subtlety when defining the blocks syntax, but decided that, barring anything obvious (which there wasn't), the improved memory management analysis of the ARC environment and/or the LLVM static analyzer made it unnecessary.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.