Reputation: 4166
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!
Upvotes: 1
Views: 196
Reputation: 64002
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
.
Upvotes: 4