Autorelease vs. dealloc release

I know that managing memory in iOS is tricky for newbies like me, but I was hoping to get a clear explanation here on stackoverflow that I couldn't find anywhere else.

So pretend that I have the / ivar property

@property(nonatomic, retain) UIPopoverController *popOver; 

which I highlight as follows:

 self.popOver = [[[UIPopoverController alloc] initWithContentViewController:popOverContent] autorelease]; 

Now, in my dealloc and viewDidUnload methods, I do both

 // in viewDidUnload: self.popOver = nil; // in dealloc: [popOver release]; 

Question:

  • If I do nil / release in viewDidUnload / dealloc, do I really need auto-advertisement on distribution?
  • And vice versa, if I do autorelease during distribution, do I need nil / release later?
  • What is the difference, if any?

Thank you in advance for your time - I will continue to read, serious memory management cannot be so difficult to wrap around you ...

+7
source share
3 answers

Do not confuse auto-advertisement in this line:

 self.popOver = [[[UIPopoverController alloc] initWithContentViewController:popOverContent] autorelease]; 

After this statement, you actually own the object , because the setter property claimed ownership of it. Car ads balance alloc-init .

So ... yes, you need auto-advertisement when posting. If you did this (without auto advertising), you would have missed:

 self.popOver = [[UIPopoverController alloc] initWithContentViewController:popOverContent]; 

Another option is to use a temporary variable instead of autorelease :

 UIPopoverController *temp = [[UIPopoverController alloc] initWithContentViewController:popOverContent]; self.popOver = temp; [temp release]; 

In any case, you need to free the object in dealloc .

+5
source

1 If I do nil / release in viewDidUnload / dealloc, do I really need auto-advertisement when distributing?

Yes.

2 And vice versa, if I do auto-distribution, do I need nil / release later?

Yes.

In the first case, automatic release is performed on behalf of this method. This method no longer needs a popover, so it needs to (auto) free it.

In dealloc your object is no longer needed. So you need to free him.

It is very simple. You do not need to consider long-term ownership of the property; you just need to think very locally, at the level of each method. The decision to release it or not does not depend at all on whether this object is stored in some other parts of the program. In a method, if you assign an object and no longer need it in this method, you (automatically) release it.

dealloc is a small exception to the rule. There you need to free ownership of all instance variables.

What is it!

+4
source
  • Yes. But perhaps not in this case.
  • Yes. But perhaps not in this case.
  • viewDidUnload is called when the viewDidUnload is unloaded, dealloc is called when the view manager is destroyed.

In viewDidUnload you free objects that are used by the view that are no longer needed and can be recreated in viewDidLoad . Obviously, since the view is not displayed, it does not need to hold the objects configured by the view controller for it.

In dealloc you clear the viewController , and here you clear all the resources, including those that were assigned to this view.

In this case, the view does not belong to the popover controller - it must belong to the view controller, so there is no need to release it in viewDidUnload , but you need to free it in dealloc .

+1
source

All Articles