The relationship between release and nil in Objective-c

Source: Internet
Author: User

 (Iphone/ipad) on the relationship between release and nil in Objective-cCategory: Iphone/ipad development technology 2011-12-09 01:40 2515 people reading reviews (4) favorite reports Uiviewcrashnull Terminal

Notice that there is often one such problem: A pointer object first release after =nil, here followed by a =nil what role? Don't write, okay?

Simply put, release is used to free memory, nil is to set the object pointer to null,nil itself has no effect on memory, but it is necessary to handle pointers, especially to avoid the wild pointer.

To give an example:

NSString *str=[[nsstring alloc] init];

When I don't need str

Execute [STR release];

The retain value of STR is reduced by 1, but if the current retain value is >0, followed by a str=nil, then [str retaincount] should be 0 because [nil retaincount]==0; But there is obviously a memory leak.

So a good writing habit is:

When the object is retaincount==1, write [str release], then write the Str=nil; This way, when the following code calls the STR-related method property again, will not error, because the STR has been set to a null pointer, and then call the Str method will also be considered null, will not be really called, and will not error.

For example, the following code: (All of the following code snippets have been verified by actual operation)

[CPP]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. UIView *view1=[[uiview alloc] init];
    2. UIView *view2=[view1 retain];
    3. int I=[view1 Retaincount];
    4. NSLog (@"i:%d", I);
    5. [View1 release];
    6. View1=nil;
    7. [View1 ADDSUBVIEW:VIEW2];

The whole program will not be crash to run. But there is a memory leak.

However, now there is another problem, see the following code:

[CPP]View Plaincopy
  1. UIView *view1=[[uiview alloc] init];
  2. UIView *view2=[view1 retain];
  3. int I=[view1 Retaincount];
  4. NSLog (@"i:%d", I);
  5. [View1 release];
  6. View1=nil;
  7. [View1 ADDSUBVIEW:VIEW2];
  8. I=[view1 Retaincount];
  9. NSLog (@"i:%d", I);
  10. I=[view2 Retaincount];
  11. NSLog (@"i:%d", I);
  12. [View2 release];
  13. I=[view2 Retaincount];
  14. NSLog (@"i:%d", I);

What should I do with the terminal output log?

The first i=2, no problem, because View1 init once, retain 1 times, the retain value is 2.

The second i=0, according to the inference above, is no problem, because the Retaincount value of the null pointer is 0 before View1=nil.

The third i=1 is not a problem, because View2 is a new value-assigned pointer, unlike pointer View1, which is 2 independent pointers, and VIEW2 is assigned a value to allocate memory addresses.

But what about the fourth one? I=?

The answer is I=1. Why is that? The following are the actual output results:

[CPP]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. 2011-12-09 01:17:07.364 releasenildemo[19115:f803] I:2
    2. 2011-12-09 01:17:07.365 releasenildemo[19115:f803] i:0
    3. 2011-12-09 01:17:07.366 releasenildemo[19115:f803] I:1
    4. 2011-12-09 01:17:07.367 releasenildemo[19115:f803] I:1

Why is that? According to reasoning, the execution of the program to the output of this sentence should be crash, I ran several times, did not appear. In the evening to discuss the problem with other programmers on the internet, some people run the situation is "sometimes crash, sometimes not." Breakpoint words will not crash ", and finally the topic of discussion began to become" system is first output or recycling first? " What's more, someone output a memory address, found that the output is the same memory address before and after the View2 release, indicating that the memory of this thing is released, but the system has not been recovered.

Sometimes crash, the reason is that you send a message to an unused memory, and eventually become a system recovery speed problem, so, personally think, later encountered similar to this problem, simply manual init,release, at least this can be released quickly, more convenient to clear the current object memory situation.

Above is my understanding of the release nil operation, welcome more people to join the discussion

The relationship between release and nil in Objective-c

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.