Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning retained object to weak property; object will be released after assignment

Tags:

ios

iphone

I used some source code :

 KGModalContainerView *containerView = 
     self.containerView = 
         [[KGModalContainerView alloc] initWithFrame:containerViewRect];

It gives me : Assigning retained object to weak property; object will be released after assignment

edit:

@interface KGModal()
  @property (strong, nonatomic) UIWindow *window;
  @property (weak, nonatomic) KGModalViewController *viewController;
  @property (weak, nonatomic) KGModalContainerView *containerView;
  @property (weak, nonatomic) UIView *contentView;
@end

KGModalContainerView *containerView = 
    self.containerView = 
        [[KGModalContainerView alloc] initWithFrame:containerViewRect];
containerView.modalBackgroundColor = self.modalBackgroundColor;
containerView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
                                 UIViewAutoresizingFlexibleRightMargin |
                                 UIViewAutoresizingFlexibleTopMargin |
                                 UIViewAutoresizingFlexibleBottomMargin;
containerView.layer.rasterizationScale = [[UIScreen mainScreen] scale];
contentView.frame = (CGRect){padding, padding, contentView.bounds.size};
[containerView addSubview:contentView];
[viewController.view addSubview:containerView];
like image 856
pengwang Avatar asked Feb 27 '13 10:02

pengwang


2 Answers

I suppose your containerView property is declared with weak attribute. If you want to have a weak attribute to a property someone should have already retained it. Here's an example:

@property (nonatomic, weak) KGModalContainerView *containerView;
...
-(void)viewDidLoad {
    [super viewDidLoad];
    KGModalContainerView *myContainerView = [[KGModalContainerView alloc] initWithFrame:containerViewRect]; // This is a strong reference to that view
    [self.view addSubview:myContainerView]; //Here self.view retains myContainerView
    self.containerView = myContainerView; // Now self.containerView has weak reference to that view, but if your self.view removes this view, self.containerView will automatically go to nil.

 // In the end ARC will release myContainerView, but it's retained by self.view and weak referenced by self.containerView
}
like image 158
graver Avatar answered Sep 22 '22 08:09

graver


My 2 cents as a beginner in Objective C:

The right hand side of the line that gives the warning,

[[KGModalContainerView alloc] initWithFrame:containerViewRect]

creates an object in the heap and at this point this object is not referenced by any pointer. Then this object is assigned to self.containerView. Because self.myContainerView is weak, the assignment does not increase the reference count of the object created on the right hand side. So when the assignment is done, the reference count of the object is still 0, and hence ARC immediately releases the object.

like image 41
Zack Zhu Avatar answered Sep 21 '22 08:09

Zack Zhu