1豁陆、對象什么時候dealloc?
當(dāng)對象的引用計數(shù)減為0時候吵护。
2盒音、dealloc發(fā)生在哪個線程表鳍?
#import "ViewController.h"
@interface classTest : NSObject
@end
@implementation classTest
- (void)dealloc{
NSThread *currentThread = [NSThread currentThread];
NSLog(@"object dealloc at thread: %@",currentThread);
}
@end
@interface ViewController ()
@property(nonatomic,strong) NSMutableArray *tArr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self dealloc_test];
}
-(void)dealloc_test{
_tArr = [NSMutableArray array];
NSThread *currentThread = [NSThread currentThread];
NSLog(@"viewDidLoad at thread: %@",currentThread);
classTest *t = [[classTest alloc]init];
[_tArr addObject:t];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[[NSThread currentThread] setName:@"DISPATCH_QUEUE_XXX"];
[self.tArr removeAllObjects];
});
}
執(zhí)行結(jié)果:
2019-07-11 xxtest[29479:1344125] viewDidLoad at thread: <NSThread: 0x6000014f5300>{number = 1, name = main}
2019-07-11 xxtest[29479:1344218] object dealloc at thread: <NSThread: 0x6000014fe700>{number = 3, name = DISPATCH_QUEUE_XXX}
dealloc并不總是在主線程中被調(diào)用,祥诽,其調(diào)用線程為最后一個調(diào)用release方法的線程譬圣。
也就是說,dealloc方法有可能在任何線程被調(diào)用。