本文是来自一位大神的分析博客,俺找不到他的博客地址了:
代码记录下来当作 自己的笔记吧
//
// main.m
// BlockDemo
//
// Created by liuxiaobing on 2018/7/25.
// Copyright © 2018 liuxiaobing. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MyClass.h"
#import "ARCBlockTest.h"
NSString *__globalString = nil;
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
// MyClass* obj = [[[MyClass alloc] init] autorelease];
// [obj test];
/**-------------------------以下测试例子请在 arc环境下进行--------------------*/
//测试testGlobalObj
// __globalString = @"1";
// void (^TestBlock)(void) = ^{
//
// NSLog(@"string is :%@", __globalString); //string is :( null)
// };
//
// __globalString = nil;
// TestBlock();
/**-------------------------end--------------------*/
//测试静态变量
// static NSString *__staticString = nil;
// __staticString = @"1";
//
// printf("38----------static address: %p\n", &__staticString); //static address: 0x6a8c
//
// void (^TestStaticBlock)(void) = ^{
//
// printf("42---------static address: %p\n", &__staticString); //static address: 0x6a8c 两者地址一致,所以当访问静态变量时,未拷贝对象指针
//
// NSLog(@"44--------string is : %@", __staticString); //string is :( null)
// };
// __staticString = nil;
// TestStaticBlock();
/**-------------------------end--------------------*/
//测试局部变量------使用local变量时,block会复制指针,且强引用指针指向的对象一次
//局部自动变量,在Block中只读。Block定义时copy变量的值,在Block中作为常量使用,所以即使变量的值在Block外改变,也不影响他在Block中的值。
NSString *__localString = nil;
__localString = @"1";
printf("local address: %p\n", &__localString); //local address: 0xbfffd9c0
ARCBlockTest *testBlock = [[ARCBlockTest alloc] init];
testBlock.test = @"64";
void (^TestBlock)(void) = ^{
printf("local address: %p\n", &__localString); //local address: 0x71723e4 两者地址不一样,说明指向对象的指针被拷贝了
NSLog(@"62---------localString is : %@", __localString); //string is : 1
NSLog(@"67--------在block 中引用局部对象,不会造成内存泄露:%@",testBlock.test);
};
__localString = nil;
TestBlock();
NSLog(@"72-------------:%@",TestBlock);
}
return 0;
}
//- (void)testGlobalObj
//{
// __globalString = @"1";
// void (^TestBlock)(void) = ^{
//
// NSLog(@"string is :%@", __globalString); //string is :( null)
// };
//
// __globalString = nil;
//
// TestBlock();
//}
//- (void)testStaticObj
//{
// static NSString *__staticString = nil;
// __staticString = @"1";
//
// printf("static address: %p\n", &__staticString); //static address: 0x6a8c
//
// void (^TestBlock)(void) = ^{
//
// printf("static address: %p\n", &__staticString); //static address: 0x6a8c
//
// NSLog(@"string is : %@", __staticString); //string is :( null)
// };
//
// __staticString = nil;
//
// TestBlock();
//}
//
//- (void)testLocalObj
//{
// NSString *__localString = nil;
// __localString = @"1";
//
// printf("local address: %p\n", &__localString); //local address: 0xbfffd9c0
//
// void (^TestBlock)(void) = ^{
//
// printf("local address: %p\n", &__localString); //local address: 0x71723e4
//
// NSLog(@"string is : %@", __localString); //string is : 1
// };
//
// __localString = nil;
//
// TestBlock();
//}
//
//- (void)testBlockObj
//{
// __block NSString *_blockString = @"1";
//
// void (^TestBlock)(void) = ^{
//
// NSLog(@"string is : %@", _blockString); // string is :( null)
// };
//
// _blockString = nil;
//
// TestBlock();
//}
//
//- (void)testWeakObj
//{
// NSString *__localString = @"1";
//
// __weak NSString *weakString = __localString;
//
// printf("weak address: %p\n", &weakString); //weak address: 0xbfffd9c4
// printf("weak str address: %p\n", weakString); //weak str address: 0x684c
//
// void (^TestBlock)(void) = ^{
//
// printf("weak address: %p\n", &weakString); //weak address: 0x7144324
// printf("weak str address: %p\n", weakString); //weak str address: 0x684c
//
// NSLog(@"string is : %@", weakString); //string is :1
// };
//
// __localString = nil;
//
// TestBlock();
//}