Autorelease vs. release in dealloc?

Don't be confused by the autorelease in this line: self. PopOver = UIPopoverController alloc initWithContentViewController:popOverContent autorelease After this statement you effectively own the object because the property setter claimed ownership of it. The autorelease balances the alloc-init So... yes, you need to autorelease at allocation.

If you did this (no autorelease), you would leak: 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 Either way you need to release the object in dealloc.

Don't be confused by the autorelease in this line: self. PopOver = UIPopoverController alloc initWithContentViewController:popOverContent autorelease; After this statement you effectively own the object because the property setter claimed ownership of it. The autorelease balances the alloc-init.So... yes, you need to autorelease at allocation.

If you did this (no autorelease), you would leak: 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; Either way you need to release the object in dealloc.

Thanks to Abizern & Yuji as well for their very enlightening answers. Also now I understood the idea of "local ownership" as described in Yuji's answer. Anyhow, is there any way to test your application for memory leaks (maybe using Instruments)?

– pille Sep 3 at 14:30 @pille Yes, you can use Instruments. In Xcode, click and hold the run button or select Product -> Profile. This brings up a dialog where you can choose "Leaks".

– albertamg Sep 3 at 15:19 @pille Even before using Instruments, just do "Analyze", which is available below "Build" menu of XCode. – Yuji Sep 4 at 8:42.

Yes. 2 Vice versa, if I do autorelease at allocation, do I need to nil / release later? Yes.In the first case, the auto-releasing is done on behalf of that method.

That method doesn't need the popover anymore, so it needs to (auto)release it. At dealloc, your object doesn't need the popover anymore. Therefore, you need to release it.

It's very simple. You don't have to consider long-term object ownership; you just need to think very locally, at the level of every method. The decision of releasing it or not doesn't at all depend on whether that object is kept by some other parts of the program.

In a method, if you alloc an object and you no longer need it in that method, you (auto)release it. The dealloc is a slight exception to the rule. There, you need to release the ownership of all the instance variables.

That's it!

Yes. But maybe not in this case. Yes.

But maybe not in this case. ViewDidUnload is called when the the view is unloaded, dealloc is called when the view Controller is being destroyed. In viewDidUnload you release objects that are used by the view that are not needed anymore and can be recreated in viewDidLoad.

Obvious, since the view is not being displayed it doesn't need to hold on to the objects that are set up by the view controller for it. In dealloc you are cleaning up the viewController and here you clear up all it's resources, including those that it has assigned to the view. In this case, the view does not own the popover controller - it should be owned by the view controller, so there is no need to release it in viewDidUnload but you do need to release it in the dealloc.

Free() is a C function that you use to dispose of no longer needed memory you obtained through malloc() or other function that is documented to require free() to deallocate its memory e.g. -dealloc is an Objective-C selector that is sent by the Objective-C runtime to an object when the object is no longer owned by any part of the application. -release is the selector you send to an object to indicate that you are relinquishing ownership of that object. Once an object is not owned by anybody, it is sent the -dealloc message by the runtime.

-autorelease is a selector you send to an object to indicate you are relinquishing ownership of the object. However if nobody now owns the object the final -dealloc will be deferred until some unspecified later point. In fact, what you are really doing is transferring your ownership to an autorelease pool which will then release it when it is itself released (or drained).

You must never send -dealloc to an object except to super in the object's own -dealloc method.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions