IOS Circular Reference Delegate (instance description)

Source: Internet
Author: User

How to avoid memory leaks caused by circular references:

Take delegate mode as an example (Viewcontroller and view are proxy modes, Viewcontroller have View rights, Viewcontroller are also agents of view (handling events in view):
  

    1. UserWebService.h
    2. #import
    3. Defines a WS-completed delegate
    4. @protocol wscompletedelegate
    5. @required
    6. -(void) finished;//the method to be implemented
    7. @end
    8. @interface Userwebservice:nsobject
    9. {
    10. ID delegate;//an ID type of dategate object
    11. }
    12. @property (assign) ID delegate;
    13. -(ID) Initwithuserdata: (user *) user;
    14. -(void) connectiondidfinishloading: (nsurlconnection *) connection;
    15. @end
    16. USERWEBSERVICE.M:
    17. #import
    18. @systhesize delegate;//Synchronize this delegate object
    19. @implementation Userwebservice
    20. -(void) connectiondidfinishloading: (nsurlconnection *) connection
    21. {
    22. [Delegate finished]
    23. }
    24. @end


LoginViewController.h:
  

    1. #import "UserWebService.h"//contains the header file containing the delegate
    2. @interface Loginviewcontroller:uiviewcontroller
    3. -(void) submit;
    4. @end
    5. LOGINVIEWCONTROLLER.M:
    6. @implementation Loginviewcontroller
    7. -(void) Submit
    8. {
    9. User *user = [[user Alloc]init];
    10. [User setuserid:@ "username"];
    11. [User setpassword:@ "password"];
    12. WS = [[Userwebservice alloc] initwithuserdata:user];
    13. Ws.delegate = self;//Set the listener for the delegate
    14. [User release];
    15. [WS-Send];
    16. }
    17. Implement the methods in the delegate,
    18. -(void) finished
    19. {
    20. Nsattry *users = [ws users];
    21. }
    22. @end


As you can see, delegate is declared as assign:
  

    1. @property (assign) ID delegate;
Copy Code


What happens if the declaration is retain?
  

    1. Loginviewcontroller Alloc a userwebservice,userwebservice agent is loginviewcontroller, so the circular reference:
    2. WS = [[Userwebservice alloc] initwithuserdata:user]; Userwebservice object reference count plus 1
    3. Ws.delegate = Self;//loginviewcontroller Object reference count plus 1


After the external frame Allocloginviewcontroller object, the reference count of the Loginviewcontroller object is 2, and after release it cannot be destroyed, resulting in a memory leak

So using assign instead of retain to declare attributes avoids circular references, and arc can be resolved with weak references

Memory detection can be inherited by Xcode with the Instrument tool, but the memory leaks caused by circular references are undetectable.

Object release to a reference count of 0, if the corresponding pointer is not assigned to nil, how to appear the wild pointer
  

    1. ClassA *a = [[ClassA alloc] init];


A = Nil;//alloc Memory no object can control it, the reference count is always 1, which creates a memory leak

A simple assignment operation does not alter the object's reference count:
  

    1. ClassA *a = [[ClassA alloc] init];
    2. ClassA *b = reference count or 1 for objects pointed to by a;//a and b

@property:

The default is @property to @property (atomic,assign)

Nonatomic: There is no corresponding atomic keyword, even if the above is written, but atomic just is the default when you do not declare this feature, you cannot take the initiative to declare this feature. Nonatomic does not support multi-threaded access, atomic has a synchronization mechanism, support multi-threaded access, if you need multi-threaded access, declared as atomic (maintain the default), otherwise declared as nonatomic, because nonatomic efficiency is much higher than atomic

About assign, retain, and copy:assign are the system default property attributes, which apply almost all variable types to OC. For variables of non-object type, assign is the only optional attribute. But if you declare a variable of an object type as assign under the reference count, you will receive a warning from the compiler at compile time. Because assign for the object attribute under reference count, just creates a weak reference (that is, a shallow copy of what is usually said). Returning samples using variables can be dangerous. When you release the previous object, the assigned object pointer becomes a headless pointer. Therefore, when declaring a property for a variable of an object type, use assign as little as possible (or not).

About the setter of the assign synthesis, it looks like this:
  

    1. -(void) Setobja: (ClassA *) a {
    2. Obja = A;
    3. }


Before delving into the retain, write the setter that declares the Retain attribute:
  

    1. -(void) Setobja: (ClassA *) A
    2. {
    3. If (Obja! = a)
    4. {
    5. [Obja release];
    6. Obja = A;
    7. [Obja retain]; Retain count plus 1 for objects
    8. }
    9. }


Obviously, in the retain setter, the variable retain once, so even if you self.obja in the program = A; Only this sentence, Obja still need to release, in order to ensure that the object retain count is correct. But if your code obja = A; Just wrote such a sentence, so here is just a shallow copy, the object's retain count does not increase, so write, you do not need to release obja in the back. The difference between the 2 sentences is that the first sentence uses the compiler-generated setter to set the value of the Obja, while the second sentence just is a simple pointer assignment
  

    1. NSString *str = [[NSString alloc] initwithstring @ "abc"];
    2. str = @ "ABCD"; '
    3. [STR release];
    4. NSLog ("%@", str);//Print out ABCD


Why didn't str become a wild pointer? Because string constants (including NSString and @ "...") have a large reference count (100k+), which is essentially not released (managed by OC itself), so string constants can be used without release

IOS Circular Reference Delegate (instance description)

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.