Iboutlet Control Memory Management

Source: Internet
Author: User

In Mac OS, if the control is connected using iboutlet without the @ property (retain) attribute, release is not required in dealloc. If the retai attribute exists, release is required.

The reason is as follows:

On Mac OS X, iboutlets are connected like this:

    1. look for a method called set :. if it exists call it.
    2. if no method exists, look for an instance variable named ,
      set it without retaining .


On iPhone OS, iboutlets are connected like this:

    1. Call [object setvalue: outletvalue forkey: @ "<outletname>"]

the behavior of set value for key is to do something like this:

    1. look for a method called set :. if it exists call it.
    2. if no method exists, look for an instance variable named,
      set it and retain it.


If you use a property, you'll fall into the"Look for a method called set <outletname> :..."Case on both platforms. If you just use an instance variable, then you'll have different retain/release behavior on Mac OS X vs iPhone
OS. There's nothing wrong with using an instance variable, you just need to deal with this difference in behavior as you switch between platforms.

It is recommended you declare properties for all of your iboutlets for clarity and consistency. The details are spelled out inMemory
Management programming guide. the basic gist is, when your nib objects are Unarchived, the nib Loading Code will go through and set all of the iboutlets using setvalue: forkey :. when you declare the memory management behavior on the property, there is no
Mystery as to what is going on. If the view gets unloaded, but you used a property that was declared as retain, you 've still got a valid reference to your textfield.


Perhaps a more concrete example wocould be useful to indicate why you shoshould use a retaining property:

I'm going to make some assumptions about the context in which you're working-I'll assume the uitextfield above is a subview of another view that is controlled by a uiviewcontroller. I will assume that at some point, the view is
Off the screen (perhaps it is used in the context of a uinavigationcontroller), and that at some point your application gets a memory warning.

so lets say your uiviewcontroller subclass needs to access its view to display it on screen. at this point, The NIB file will be loaded and each iboutlet properties will be set by the nib loading code using setvalue: forkey :. the
important ones to note here are the top level view that will be set to the uiviewcontroller's view property, (which will retain this top level view) and your uitextfield, which will also be retained. if it is simply set, it'll have a retain put on it by the
nib Loading Code, otherwise the property will have retained it. the uitextfield will also be a subview of the top level uiview, so it will have an additional retain on it, being in the subviews array of the top level view, so at this point the text field has
been retained twice.

At this point if you wanted to switch out the text field programmatically, you coshould do so. using the property makes memory management more clear here; you just set the property with a new autoreleased text field. if you had not
Used the property, you must remember to release it, and optionally retain the new one. at this point it is somewhat ambiguous as to whom owns this new text field, because the memory management semantics are not contained within the setter.


Now let's say a different view controller is pushed on the uinavigation controller's stack, so that this view is no longer in the foreground. in the case of a memory warning, the view of this offscreen View Controller will be unloaded.
At this point, the view property of the top level uiview will be nulled out, it will be released and deallocated.

Because the uitextfield was set as a property that was retained, the uitextfield is not deallocated, as it wowould have been had its only retain been that of the subviews array of the top level view.

If instead the instance variable for the uitextfield not been set via a property, it 'd also be around, because the nib Loading Code had retained it when setting the instance variable.

one interesting point this highlights is that because the uitextfield is additionally retained through the property, you'll likely not want to keep it around in case of a memory warning. for this reason you shoshould nil-out the property
In the-[uiviewcontroller viewdidunload] method. this will get rid of the final release on the uitextfield and deallocate it as intended. if using the property, you must remember to release it explicitly. while these two actions are functionally equivalent,
the intent is different.

If instead of swapping out the text field, you chose to remove it from the view, you might have already removed it from the view hierarchy and set the property to nil, or released the text field. while it is possible to write
Correct program in this case, its easy to make the error of over-releasing the text field in the viewdidunload method. over-releasing an object is a crash-inducing error; Setting a property that is already nil again to nil is not.

My description may have been overly verbose, but I didn't want to leave out any details in the scenario. simply following the guidelines will help avoid problems as you encounter more complex situations.

It is additionally worth noting that the memory management behavior differs on Mac OS X on the desktop. on the desktop, setting an iboutlet without a setter does not retain the instance variable; but again uses the setter if available

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.