• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Threaded drawing
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Threaded drawing (From: Graham Cox <email@hidden>)
 >Re: Threaded drawing (From: Graham Cox <email@hidden>)
 >Re: Threaded drawing (From: Graham Cox <email@hidden>)
 >Re: Threaded drawing (From: Jens Alfke <email@hidden>)

  • Prev by Date: Re: Threaded drawing
  • Next by Date: Re: Threaded drawing
  • Previous by thread: Re: Threaded drawing
  • Next by thread: Re: Threaded drawing
  • Index(es):
    • Date
    • Thread