문제

I have a question. There may be a very simple solution to this question but I am not able to figure it out yet. If I use a property say @property(nonatomic, retain)UIView *mainView.

Now I synthesize it in .m file and release it in the dealloc method as following:

- (void)dealloc {
   [mainView release], mainView = nil;
   [super dealloc];
}

Then in my viewDidLoad, I'm allocating it and adding it as the subview of my self.view like following:

- (void) viewDidLoad {

  mainView = [[UIView alloc] init];
  .
  .
  .
  [self.view addSubView: mainView];
}

Now I understand that at this point my mainView would have 3 reference counts (one from alloc, one because it's a retained property, and the third one when I added it to self.view), its parent controller would own it too. Now, my question is if after adding my view to self.view, I release my mainView using

[mainView release];

My app crashes when I go back to the previous view as I am sending release to already deallocated object. Now my question is how am I overreleasing my view here. what am I missing because when I use following code it works fine and no crashes occur.

- (void) viewDidLoad {
  UIView *newView = [[UIView alloc] init];
  self.mainView = newView;
  [newView release];
  .
  .
  .
  [self.view addSubView: mainView];
}

I know why this second viewDidLoad method works but what I dont know is why first one fails, I am supposed to release my view after adding it to self.view. Right?

NOTE: I understand that in the first viewDidLoad, I can use autorelease method to release the view that is being assigned to the ivar and it wont crash but the whole point is I am trying to reduce the use of autorelease as much as possible. And I am not using ARC at all

I would really appreciate the explanation and suggestions.

도움이 되었습니까?

해결책 2

In order for your @property to retain the mainView, you should use it as self.mainView and not just mainView. If you use the latter alone, it will not retain it. Basically if you call self.mainView = ... it is calling a setter method for mainView which does a [mainView retain]; internally. When you are directly assigning it, it wont execute this setter and retain will not be executed.

You should try it like this,

self.mainView = [[[UIView alloc] init] autorelease];
[self.view addSubView:self.mainView];

or as shown in your question.

UIView *newView = [UIView alloc] init];
self.mainView = newView;
[newView release];
[self.view addSubView:self.mainView];

You can also try using ARC for your project. Your code will look like this in ARC,

self.mainView = [[UIView alloc] init];
[self.view addSubView:self.mainView];

Check the documentation for more details.

다른 팁

From your question:

Now i understand that at this point my mainView would have 3 reference counts (one from alloc, one coz its a retained property and the third one when i added it to self.view)

You didn't assign through the property but assigned directly to the instance variable, so there was no retain; only in ARC does assigning to the instance variable retain the value. So, do not perform the manual release.

In your first viewDidLoad method you are not referring to self.mainView only mainView thats why its not retained, in order to retain property work you have to set mainView using self.mainView!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top