As I understand ARC, without a strong reference to an object, it is fair game to be collected (since its reference count is 0).
If, in a method in class A, I do this:
ClassB* b = [[ClassB alloc] init];
[b doStuff];
And in doStuff, I do this:
NSThread* t = [[NSThread alloc] initWithTarget:self selector:@selector(theThread) object:nil];
[t start];
The reference count of b
appears to be 0 since it went out of scope after the method in class A finishes. However, a thread is currently 'running' in ClassB and will need local resources.
What is the behavior here? Or perhaps, what should the behavior here be to make sure that b
stays around until the thread is all finished?
Thanks!
The documentation for initWithTarget:selector:object:
says that the thread takes ownership of (keeps a strong reference to) its target. The target will be released when the thread object is destroyed.
Be aware that this can cause a retain cycle, if the target also owns the NSThread
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.