1

I have declared the variables as a subclass of UIViewExtention. Am converting the project non-arc to arc

@interface SectionIconView : UIViewExtention 
@property (nonatomic,weak)  UIImageView *sectionPortraitImageView;
@property (nonatomic,weak)  UIImageView *sectionLandscapeImageView;

I have declared the property as weak, then it shows an error in .m file i.e.,

Assigning retained object to weak variable;object will be released after assignment.

I have changed the attribute as strong..

@property (nonatomic,strong)  UIImageView *sectionPortraitImageView;

Then it shows an error is:

Capture strongly in this block is likely to lead to a retain cycle.

How to avoid this error?

Jyoshna
  • 243
  • 3
  • 14
  • 2
    Update your question with the block causing the message. – rmaddy Dec 16 '14 at 06:47
  • Make use of weak or autorelease objects in block.... Else it will lead to retain cycle... – Leena Dec 16 '14 at 06:55
  • @Leena I have added the property `@property (copy) void (^blockProperty)(void);` ` __weak UIImageView *sectionLandscapeImageView = self; self.blockProperty = ^{ sectionLandscapeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xPos, yPos, width, height)]; }; ` Still, capturing "self" strongly in a block issue arises.. and also one more issue `Incomapitble pointer type` it is a subclass of UIViewExtension – Jyoshna Dec 16 '14 at 07:17
  • __weak UIImageView *sectionLandscapeImageView = self. sectionPortraitImageView; use this instead of assigning self. You just copy pasted the code which @lanLovejoy pasted please refer the link that I have provided for further understanding as why the warning is coming...... – Leena Dec 16 '14 at 07:25
  • @Leena Most of the time you want strong references in the block, including to self. There are some situations however where it needs to be unique. There is a reason why the default is strong. – Abhi Beckert Dec 16 '14 at 07:57

1 Answers1

2

Please see this Apple documentation on how to avoid capturing "self" strongly in a block. Here's the key part:

XYZBlockKeeper * __weak weakSelf = self;
self.block = ^{
    [weakSelf doSomething];   // capture the weak reference
                              // to avoid the reference cycle
}

If you need to access ivars within your block you will need the following additional pattern

self.block = ^{
    XYZBlockKeeper * strongSelf = weakSelf;
    // can now access strongSelf->myIvar safely
}

You might think you could use weakSelf->myIvar but that will lead to yet another warning about a race condition. The above code will also ensure that self stays alive while your block runs, which may be desirable even if you aren't accessing ivars.

Finally, if your code make it possible you might consider not capturing self at all, just capturing what you need, which avoids a lot of this complexity:

MyClass *myThing = self.myThing;
self.block = ^{
    // use myThing here, self is not captured
}
Ian Lovejoy
  • 376
  • 3
  • 7
  • Note: the key part is `self.block =`. That is mostly the only time you need a weak reference to self. – Abhi Beckert Dec 16 '14 at 07:02
  • @AbhiBeckert Stil the issue arises exhibitLandscapeImageView property i declared in .h with the strong attribute `self.blockProperty = ^{ [exhibitLandscapeImageView setImage:landscapeImage];};` – Jyoshna Dec 16 '14 at 08:12
  • If i changed the attribute to weak, I got the another issue like `Assigning retained object to weak variable;object will be released after assignment. ` – Jyoshna Dec 16 '14 at 08:14
  • @Jyoshna, you want the property that stores the block to be strong. If you are still seeing the warning, look for another, possibly implicit reference to "self", probably an ivar. For example if "landscapeImage" in your code above is an ivar, that counts as a reference to "self". If you have to reference an ivar, you can use this additional pattern within your block: ^{XYZBlockKeeper *strongSelf = weakSelf; strongSelf->myiVar;} The declaration of the additional "strongSelf" within the block will keep "self" alive while the block is running only, and so won't lead to a retain cycle. – Ian Lovejoy Dec 16 '14 at 16:50
  • Updated answer to reflect what's in my comment – Ian Lovejoy Dec 16 '14 at 17:06
  • I see this issue has been marked as a duplicate. It would really be nice however if the accepted answer in the other question covered the -> notation for ivars, which some developers are not familiar with. – Ian Lovejoy Dec 16 '14 at 17:37