- (Void) setEngine: (Engine *) newEngine {
engine = [newEngine retain];
} / / Car class a setter method
main method of operation code:
Engine * engine1 = [Enging new] ;/ / count: 1
[Car setEngine: engine1] ;/ / count: 2
[Engine1 release] ;/ / count: 1
Engine * engine2 = [Enging new] ;/ / count: 1
[Car setEngine: engine2] ;/ / count: 2
This book on the code in question is this description:
engine1 have a problem, because it's retain count is still 1.
main method has been released on engine1 references, but Car is not.
engine1 is missing, then engine1 this object will always exist and occupy part of the memory.
second case:
- (Void) setEngine: (Engine *) newEngine {
[Engine release];
engine = [newEngine retain];
} / / Car class a setter method
The wording of the first case to solve the problems, but when faced with the following situation, there would be problems
Engine * engine = [Enging new] ;/ / count: 1
Car * car1 = [Car new];
Car * car2 = [Car new];
[Car1 setEngine: engine] ;/ / count: 2
[Engine release] ;/ / count: 1
[Car2 setEngine: [car1 engine]] ;/ / oops
The book's original description of the problem is this:
[Car1 engine] returns a retain count of the engine as a pointer.
The first line of setEngine been implemented [engine release] Let engine variables
retain count becomes 0, and this engine object has been recovered. So now newEngine
And engine instance variables are pointing to a period has been freed memory causes a program error.
The above paragraphs code must read "Learn Objective-C on the Mac" book of children's shoes have seen it.
What I do not understand is that by learning objective-c, when an object's retain count value of 0 indicates that the object has been recovered,
My question 1:
If I setEngine: Methods in write directly:
engine = newEngine;
That first case mentioned problem should not exist, right?
My question 2:
engine = newEngine;
And
engine = [newEngine retain];
What is the difference
Another problem is:
If the project uses the ARC after piece about memory management system can be completely handed over to deal with it?
Reply:
Your question 1:
If you are not new to the value of the retain, then you simply refer to it without ownership, which is a weak reference; so what happens then? Is when your newEngine released when your engine is also gone. Then there will be EXC_BAD_ACCESS;
Your question 2:
@ 1 engine = newEngine; weak references
And
@ 2 engine = [newEngine retain]; strong references
@ A simply pointing newEngine, does not newEngine ownership, when newEngine freed after, engine while also being released
@ 2 is a strong reference, when newEngine retain after, newEngine the reference count of +1, so to speak, newEngine have a cable connected to the XX this memory area while engine also has a cable connected to the XX this memory area, when [ newEngine release] after just newEngine to the XX line was shut off, and the engine did not break the root, so the engine can continue to use.
Another problem you is:
Using the ARC is not to say that the memory management to the system. When using ARC environment, you must explicitly show the current ownership of this variable, use __autoreleasing
__strong
__unsafe_unretained
__weak
These indicate that the current right to ownership of the objects. And like the Core Foundation is not involved in such libraries ARC, you still must use the CFRelease (), etc. These methods to release your CF Object.
In summary, if you are using ARC, but still said the ownership of the object is not clear, will still be a memory leak.
Reply:
Thank you for your answer, very detailed, very easy to understand. By your answer, that I was not the first case can be understood:
In the first case the second setEngine executed, in fact, this time the car inside the engine while pointing engine1 and engine2, or is engine1 at this time is in the free state, engine2 have been engine.
There is a want to ask about the iOS development, memory management to handle this is to manually or using the ARC is good, or that the project is based on the actual situation to decide.
Reply:
Do not know whether to use ARC, how to use
Reply:
http://dev.10086.cn/cmdn/wiki/index.php?doc-view-3560.html
Reply:
http://shy818818.blog.163.com/blog/static/933983092011328111510158/
No comments:
Post a Comment