According to this page :
In general, you can use C ++ objects inside a block. Inside a member of a function, references to member variables and functions through an implicitly imported this pointer and therefore mutable displayed. There are two considerations that apply if a block is copied:
- If you have a __block storage class for what would be based on a C ++ object, then the usual copy constructor is used.
- If you use any other C ++-based object from within the block, it must have a const const constructor. Then the C ++ object is copied using this constructor.
Empirically, I observe that it const copies the this pointer into a block. If the C ++ instance pointed to by this is no longer at that address when the block is executed (for example, if the Worker instance that Worker::Work() is called on has been stacked on a higher frame), you will get EXC_BAD_ACCESS or worse (i.e. smoothing the pointer). Thus, it seems that:
- It writes
this , rather than copying instance variables by value. - Nothing is done to make the object pointed to by
this alive.
Alternatively, if I reference a C ++ object with a local stack (i.e. declared in this frame / stack area), I see that its copy constructor is called when it is initially captured by the block, and then again when the block (for example, in the operation queue when queuing the operation).
To specifically ask your questions:
But here we fix the current value of this ? Copy this using Workers copy constructor? Or a link to where node is stored?
We write this . Consider this const-copy intptr_t if that helps.
The fred object may not exist when the block starts. What is the meaning of node ? (Suppose the base node objects live forever, but the workers come and go.)
In this case, this was written by value, and node actually a pointer with the value this + <offset of node in Worker> , but since the Worker instance does not work, it is actually a garbage pointer.
I would not do any magic or other behavior besides what is described in these documents.
ipmcc
source share