Re: Block Confusion...
Re: Block Confusion...
- Subject: Re: Block Confusion...
- From: Fritz Anderson <email@hidden>
- Date: Tue, 31 Jan 2012 09:07:44 -0600
On 30 Jan 2012, at 8:56 PM, R wrote:
> One class creates a STRONG pointer to a NSMutableArray. It then
> passes that pointer to another class, which has a WEAK reference.
> that class is listed below. When I fork a new thread for the
> simpleBlock, I lose the pointer. I tried this on a fresh, very simple
> test and I did not have this behavior. It behaved as expected with
> the same memory address for all three logs.
I'll call "one class" ClassA, and "another class" ClassB.
Instances of ClassA have a strong reference to an NSMutableArray. Instances of ClassB borrow the array through a weak reference.
I think if you create a -[ClassA dealloc] method to log the deallocation of a ClassA, you'll find that the ClassA object is deallocated before the enqueued block uses the ClassB reference to the array. In a simple case (no other object has a strong reference to the array), the array gets deallocated, too. ClassB's reference being weak, the reference is nilled-out.
The call to [self checkMutableArray] causes the block to retain self, not the weakly-referenced array. The call isn't resolved until the block is run.
The other case worked only by chance. You should treat the timing and order of deallocation as though they were nondeterministic.
— F
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden