IOS memory release rules

Source: Internet
Author: User

  1. When you create an object using new, alloc, or copy, the Count retain of the object is 1. You must delete this object release or autolease. In this way, it can be cleared only when its lifecycle ends.
    When you create an object using new, alloc, or copy, the object has a retain count of 1. you are responsible for sending the object a release or autorelease message when you're done with it. that way, it gets cleaned up when its useful life is over.
  2. When you use other methods to obtain an object, you can think that it has retained a count and autolease has fallen. You don't have to worry about cleaning problems related to it. But if you want to keep this object, you need to retain it and make sure that you release it later.
    When you get hold of an object via any other mechanic, assume it has a retain count of 1 and that it has already been autoreleased. you don't need to do any fur-ther work to make sure it gets cleaned up. if you're going to hang on to the object for any
    Length of time, retain it and make sure to release it when you're done.
  3. If you retain an object, you always need release or autolease.
    If you retain an object, You Need To (eventually) release or autorelease it. balance these retains and releases.

These three rules must be followed when writing code. Once followed, there will be no memory leakage.

Create object

In objective-C, you can create an object in two steps: alloc and init. alloc initializes the memory to the object variable on the heap and sets the variable (pointer) to nil. Each class can have many init methods, and each method starts with init, but each class has only one specific (designated) Init method, nsobject is Init; and uiview is-(ID) initwithframe :( cgrect) arect ;. In the designated method of the subclass, you must call the designated method of the parent class.
The init method can only call the Child class's own designated method, and cannot call the parent class (that is, use self instead of super ).

Below are some tips:

  1. When you want to retain objects temporarily, use autolease
    -(Money *) showmethemoney :( double) Amount {
    Money * Themoney = [[money alloc] init: amount];
    [Themoney autorelease];
    Return Themoney;
    }
  2. Autolease of the Collection class. One method is to call autolease like an object. Alternatively, you can call [nsmutablearray array]. The best way is to return [nsarray arraywithobjects: @ "Steve ", @ "Ankush", @ "Sean", nil]; The objects returned by other similar methods are all autolease objects.
    [Nsstring stringwithformat: @ "Meaning of % @ is % d", @ "life", 42];
    [Nsdictionary dictionarywithobjectsandkeys: Ankush, @ "ta", janestudent, @ "student", nil];
    [Nsarray arraywithcontentsoffile :( nsstring *) path];
  3. The Collection class has ownership for the newly added object.
  4. @ "String" is autorelease's
  5. Nsstring is generally copye rather than retain
  6. You should release your objects as soon as possible. The sooner the better. We recommend that you write the release code after creating the object.
  7. After the owner release of the last object, the dealloc function is automatically called, and the dealloc function needs to be reloaded in the class, but never call the dealloc function by yourself.
  8. @ Property returns the object variable directly. We can regard it as an autolease object.
  9. When @ synthesize is used, @ property can specify that the setter function uses retain, copy, or assign. Generally, assign is used in attributes that will inevitably die with the object's extinction, such as the controller's view and view's delegate.
  10. Protocols can be understood as an abstract interface. The delegat and datasource are basically defined by protocol.

1. Keep the Count of objects created by allocation or replication 1
2. Assume that the object obtained by any other method is counted as 1 and is in the Auto Release pool. To use this object outside the current execution range, you must keep it
3. It is maintained when an object is added to the set, and is released when the object is removed from the set. Releasing the set object will release all objects in the set.
4. Make sure that the number of alloc, copy, mutablecopy, or retain messages is the same as the number of release or autorelease messages that are sent to this object. In other words, ensure that your code is balanced.
5. Set the attribute in the access method and keep it before releasing (ztime: @ propperty and @ synthesize commands automatically create this code)
6. The nsstring object created with the @ "..." structure is a constant. Sending release or retain has no effect.

Related Article

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.