[toc]
參考
OC代碼
int main(int argc, const char * argv[]) {
__block NSMutableString *str = [[NSMutableString alloc] initWithString:@"Mercedes"];
NSLog(@"0_str = %@ - %p - %p", str, str, &str);
void (^myBlock)(void) = ^{
NSLog(@"1_str = %@ - %p - %p", str, str, &str);
[str appendString:@"-Benz"];
NSLog(@"2_str = %@ - %p - %p", str, str, &str);
str = [[NSMutableString alloc] initWithString:@"Daimler"]; // 改變指針指向
NSLog(@"3_str = %@ - %p - %p", str, str, &str);
};
NSLog(@"4_str = %@ - %p - %p", str, str, &str);
myBlock();
NSLog(@"5_str = %@ - %p - %p", str, str, &str);
return 0;
}
MRC 輸出: (str的值 - str指向的堆中的地址 - str的指針地址)
0_str = Mercedes - 0x103805820 - 0x7ffeefbff458
4_str = Mercedes - 0x103805820 - 0x7ffeefbff458
1_str = Mercedes - 0x103805820 - 0x7ffeefbff458
2_str = Mercedes-Benz - 0x103805820 - 0x7ffeefbff458
3_str = Daimler - 0x1007b67a0 - 0x7ffeefbff458
5_str = Daimler - 0x1007b67a0 - 0x7ffeefbff458
ARC 輸出:
0_str = Mercedes - 0x103305460 - 0x7ffeefbff458
4_str = Mercedes - 0x103305460 - 0x103305818 // 變量指針地址在block定義之后, 就變成了 堆地址
1_str = Mercedes - 0x103305460 - 0x103305818 // 變量指針地址變?yōu)槎训刂?2_str = Mercedes-Benz - 0x103305460 - 0x103305818
3_str = Daimler - 0x103305770 - 0x103305818
5_str = Daimler - 0x103305770 - 0x103305818
C++代碼
int main(int argc, const char * argv[]) {
__attribute__((__blocks__(byref))) __Block_byref_str_0 str = {(void*)0,(__Block_byref_str_0 *)&str, 33554432, sizeof(__Block_byref_str_0), __Block_byref_id_object_copy_131, __Block_byref_id_object_dispose_131, ((NSMutableString *(*)(id, SEL, NSString * _Nonnull __strong))(void *)objc_msgSend)((id)((NSMutableString *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("NSMutableString"), sel_registerName("alloc")), sel_registerName("initWithString:"), (NSString *)&__NSConstantStringImpl__var_folders_p5_mp3284bs2xb073r91w__n99r0000gn_T_main_a5b5be_mi_0)};
NSLog((NSString *)&__NSConstantStringImpl__var_folders_p5_mp3284bs2xb073r91w__n99r0000gn_T_main_a5b5be_mi_1, (str.__forwarding->str), (str.__forwarding->str), &(str.__forwarding->str));
void (*myBlock)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, (__Block_byref_str_0 *)&str, 570425344));
NSLog((NSString *)&__NSConstantStringImpl__var_folders_p5_mp3284bs2xb073r91w__n99r0000gn_T_main_a5b5be_mi_7, (str.__forwarding->str), (str.__forwarding->str), &(str.__forwarding->str));
((void (*)(__block_impl *))((__block_impl *)myBlock)->FuncPtr)((__block_impl *)myBlock);
NSLog((NSString *)&__NSConstantStringImpl__var_folders_p5_mp3284bs2xb073r91w__n99r0000gn_T_main_a5b5be_mi_8, (str.__forwarding->str), (str.__forwarding->str), &(str.__forwarding->str));
return 0;
}
struct __Block_byref_str_0 {
void *__isa;
__Block_byref_str_0 *__forwarding;
int __flags;
int __size;
void (*__Block_byref_id_object_copy)(void*, void*);
void (*__Block_byref_id_object_dispose)(void*);
NSMutableString *__strong str;
};
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
__Block_byref_str_0 *str; // by ref
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, __Block_byref_str_0 *_str, int flags=0) : str(_str->__forwarding) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
__Block_byref_str_0 *str = __cself->str; // bound by ref
NSLog((NSString *)&__NSConstantStringImpl__var_folders_p5_mp3284bs2xb073r91w__n99r0000gn_T_main_a5b5be_mi_2, (str->__forwarding->str), (str->__forwarding->str), &(str->__forwarding->str));
((void (*)(id, SEL, NSString * _Nonnull __strong))(void *)objc_msgSend)((id)(str->__forwarding->str), sel_registerName("appendString:"), (NSString *)&__NSConstantStringImpl__var_folders_p5_mp3284bs2xb073r91w__n99r0000gn_T_main_a5b5be_mi_3);
NSLog((NSString *)&__NSConstantStringImpl__var_folders_p5_mp3284bs2xb073r91w__n99r0000gn_T_main_a5b5be_mi_4, (str->__forwarding->str), (str->__forwarding->str), &(str->__forwarding->str));
(str->__forwarding->str) = ((NSMutableString *(*)(id, SEL, NSString * _Nonnull __strong))(void *)objc_msgSend)((id)((NSMutableString *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("NSMutableString"), sel_registerName("alloc")), sel_registerName("initWithString:"), (NSString *)&__NSConstantStringImpl__var_folders_p5_mp3284bs2xb073r91w__n99r0000gn_T_main_a5b5be_mi_5);
NSLog((NSString *)&__NSConstantStringImpl__var_folders_p5_mp3284bs2xb073r91w__n99r0000gn_T_main_a5b5be_mi_6, (str->__forwarding->str), (str->__forwarding->str), &(str->__forwarding->str));
}
static struct __main_block_desc_0 {
size_t reserved;
size_t Block_size;
void (*copy)(struct __main_block_impl_0*, struct __main_block_impl_0*);
void (*dispose)(struct __main_block_impl_0*);
} __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0), __main_block_copy_0, __main_block_dispose_0};
static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {
_Block_object_assign((void*)&dst->str, (void*)src->str, 8/*BLOCK_FIELD_IS_BYREF*/);
}
static void __main_block_dispose_0(struct __main_block_impl_0*src) {
_Block_object_dispose((void*)src->str, 8/*BLOCK_FIELD_IS_BYREF*/);
}