(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" >
- UIView *view1=[[uiview alloc] init];
- UIView *view2=[view1 retain];
- int I=[view1 Retaincount];
- NSLog (@"i:%d", I);
- [View1 release];
- View1=nil;
- [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
- UIView *view1=[[uiview alloc] init];
- UIView *view2=[view1 retain];
- int I=[view1 Retaincount];
- NSLog (@"i:%d", I);
- [View1 release];
- View1=nil;
- [View1 ADDSUBVIEW:VIEW2];
- I=[view1 Retaincount];
- NSLog (@"i:%d", I);
- I=[view2 Retaincount];
- NSLog (@"i:%d", I);
- [View2 release];
- I=[view2 Retaincount];
- 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" >
- 2011-12-09 01:17:07.364 releasenildemo[19115:f803] I:2
- 2011-12-09 01:17:07.365 releasenildemo[19115:f803] i:0
- 2011-12-09 01:17:07.366 releasenildemo[19115:f803] I:1
- 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