^Block statement considered harmful for callbacks?
^Block statement considered harmful for callbacks?
- Subject: ^Block statement considered harmful for callbacks?
- From: Oleg Krupnov <email@hidden>
- Date: Thu, 25 Apr 2013 11:20:41 +0300
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.
Thoughts?
_______________________________________________
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