Re: Threaded drawing
Re: Threaded drawing
- Subject: Re: Threaded drawing
- From: Greg Parker <email@hidden>
- Date: Fri, 06 Dec 2013 17:38:40 -0800
On Dec 6, 2013, at 11:37 AM, Jens Alfke <email@hidden> wrote:
> On Dec 6, 2013, at 7:27 AM, Graham Cox <email@hidden> wrote:
>> Is the value of <tileRect> here captured when the block is created,or when it is run?
>
> It depends on whether tileRect is an instance variable.
> * If it isn’t (i.e. it’s local/static/global), it gets captured when the block is created.
> * If it _is_ an ivar, then “tileRect” is just syntactic sugar for “self->tileRect”, which means that ‘self’ gets captured at create time, and the ‘->tileRect’ part is evaluated at runtime.
*Only* ordinary local variables are captured when the block is constructed. Globals, static locals, __block locals, and ivars are not captured.
% clang test.m -framework Foundation && ./a.out
local 0, static_local 1, block_local 1, global 1, ivar 1
% cat test.m
#include <Foundation/Foundation.h>
@interface Test : NSObject @end
int global;
@implementation Test {
int ivar;
}
-(void)method
{
int local;
static int static_local;
__block int block_local;
local = 0;
static_local = 0;
block_local = 0;
global = 0;
ivar = 0;
void (^block)(void) = ^{
printf("local %d, static_local %d, block_local %d, global %d, ivar %d\n",
local, static_local, block_local, global, ivar);
};
local = 1;
static_local = 1;
block_local = 1;
global = 1;
ivar = 1;
block();
}
@end
int main()
{
[[Test new] method];
}
--
Greg Parker email@hidden Runtime Wrangler
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden