Inspection to detect a strong reference in a block
We've got some code that uses blocks, however, the implementor didn't ensure that references to self in the block were weak, thus causing a retain cycle.
Is it possible to flag code such as:
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[actionSheet addButtonWithTitle:@"Take a Photo" usingBlock:^
{
[self showProfilePicture:self.server.profile from:MCTakePictureSourceTypeCamera];
}];
}
with at least a warning that this code could/will cause a retain cycle. The appropriate implementation for this, pre-arc, is this:
__block MCProfileWireframe* blockSelf = self;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[actionSheet addButtonWithTitle:@"Take a Photo" usingBlock:^
{
[blockSelf showProfilePicture:blockSelf.server.profile from:MCTakePictureSourceTypeCamera];
}];
}
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[actionSheet addButtonWithTitle:@"Take a Photo" usingBlock:^
{
[blockSelf showProfilePicture:blockSelf.server.profile from:MCTakePictureSourceTypeCamera];
}];
}
Thanks.
Please sign in to leave a comment.
Seamus, we'll add such an inspection in future, here is a request for you to watch and vote: http://youtrack.jetbrains.com/issue/OC-4088
Thanks!
Good stuff.
I do want to let your team know that the ability to submit bugs and feature requests, and track them, is a huge plus for me. I list it as a feature when trying to convince folks to try AppCode.