Re: ^Block statement considered harmful for callbacks?
Re: ^Block statement considered harmful for callbacks?
- Subject: Re: ^Block statement considered harmful for callbacks?
- From: Oleg Krupnov <email@hidden>
- Date: Thu, 25 Apr 2013 11:34:52 +0300
Tom, I disagree, because unlike other objects with strong refs, or say
@property(retain), the strong refs in blocks are created implicitly
and it's just too easy to let them slip out of attention. There is no
direct command to create the strong ref, unlike @property(retain). All
you do is *mention* self or even an ivar, and voila, you're done.
I wish there was a safe and elegant solution to this, like we did with
proxy for NSTimer.
On Thu, Apr 25, 2013 at 11:28 AM, Tom Davie <email@hidden> wrote:
>
> On 25 Apr 2013, at 09:20, Oleg Krupnov <email@hidden> wrote:
>
>> Blocks in Obj-C seem convenient and powerful but in fact they are very
>> dangerous because they retain all objects referenced from the block
>> and thereby can implicitly create circular references.
>>
>> If you are not vigilant about every object you mention in the block,
>> you can create one, two or more cycles of circular references causing
>> your objects to never be freed afterwards.
>>
>> This breaks encapsulation of objects with block properties (e.g.
>> MyAnimation.completionBlock) and forces the users of such objects to
>> always keep in mind what they can, and what the cannot write in the
>> block body, and still they can shoot themselves in the feet at any
>> moment by losing vigilance.
>>
>> It seems to me that it's much better to drop the convenience of blocks
>> in favor of safety and full control of retain/assign relationships
>> between my objects.
>>
>> This is a shocker for me too, but I do not see a good solution to this
>> problem. The solution suggested in Session 712 WWDC 2012 is ugly -
>> call some kind of "cancel" method for every block when it's no longer
>> needed. Even with ARC, declaring a weak ref to self and then strong
>> ref to weak ref inside the block is ugly too - you have to add this
>> crap to each and every block you write.
>
> Your argument can equally well be applied to any object that keeps strong references to other objects. It's not blocks that are doing any of the above, it's reference counting. And we know that the issues are there – we just choose to have them because the issues with the less leaky solutions are even more severe (especially in C like languages).
>
> Thanks
>
> Tom Davie
_______________________________________________
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