Saturday, December 7, 2013

Retention and release method of memory management issues, as detailed in the program


Access methods to retain and release:
Principle is the best way to set up (to keep the new release old, who owns the object whoever is responsible for releasing the object)
- (Void) setEngine: (Engine *) newEngine
{
[NewEngine retain];
[Engine release];
engine = newEngine;
}

I ask, where [newEngine retain]; [engine release]; How to understand?
Reply:
Found out, huh.
If you first release, "and then" only retain, you'll give yourself deconstruction (destruct, with respect to the construction of)! This is why it should be
1) retain 2) the reasons for release 3) set values.

So, now we need to release it, is still on the engine newEngine be release, or both, the effect is the same?
Reply:
This time full of.

- (Void) setCity: (NSString *) c {
[C retain] ;/ / --- 1
[City release] ;/ / --- 2
city ​​= c ;/ / --- 3
}
In the JAVA language using GC mechanism, we just need to write the third statement. Object-C in fact you can
Write only the third statement, city we obtained in this way is called the tube weak reference, which is the city only through assignment,
Himself pointed object c point, but the object itself reference counter without any change at this city must bear
Risk [c release] brought after, but in some cases, you may indeed need this weak references. Here you need
Note that some documents to this weak references called assign (assign, this is the image directly assigned to a pointer to another
A pointer to the object, but does not have the object) mode, called retain strong references way.
Of course, in most cases, are strong references we use, that is, using the first lines of code retain it, so
Plus a reference counter, then assignment, said further object is to first get through retain ownership of the method,
Re safe to use objects.
Reply:
The second line of code is why? In fact, this is to prevent the city may have been pointing to an object, if not the first of
city ​​once release, but directly to the city c pointing to the object, then the object may point to the original city of
Now a memory leak, because the city is changing point of time, there is no reference to the original object pointed to the counter by 1, in violation of
After retain the object, to ask you to release the object at the right time.
The third line of code in doubt appears to be in the last line, but as previously described, seemingly the first line, second line of code
There is no sequence, that is, you can first [city release], then [c retain], but is it really? There is a more
Rare cases, that is to own their own assigned as an argument (that sounds weird), that the parameters c and member variable
city ​​is a pointer variable, then the first time if you call the [city release], will lead retainCount objects owned by 0,
Dealloc object, then [c retain] will complain because the object is gone.
In summary, the three lines of code shown above is a better organization. But in fact, you may also see some other
The way to write a setter method, which is also no surprise, as long as the object of normal use, recycling is not asked
Question of the code.
Reply:
LZ self-taught, gratifying
Reply:
LZ self-taught, gratifying
Reply:
- (Void) setMyField: (NSString *) newValue {
if (newValue! = myField) {
[MyField release];
myField = [newValue retain];
}
}
Reply:
references, 4th Floor reply:
LZ autodidact gratifying
amount, Thank you for saying that the original can not see the technology website Drink plenty of water, the water was almost up to my keyboard
Reply:
references 6th Floor reply:
- (void) setMyField: (NSString *) newValue {
if (newValue! = myField) {
[MyField release];
myField = [newValue retain];
}
}
This is to show himself as a parameter is assigned to deal with their own situation, also looks good look.
Reply:
referenced 8th Floor reply:
references 6th Floor reply:
- (Void) setMyField: (NSString *) newValue {
if (newValue! = myField) {
[MyField release];
myField = [newValue retain];
}
}
This was to show himself as a parameter is assigned to deal with their own situation, also looks good like.

This is @ property (retain) code
Reply:
reference to the 9th floor Reply:
reference to the 8th floor of replies:

6th Floor cited reply:
- (Void) setMyField: (NSString *) newValue {
if (newValue! = myField) {
[MyField release];
myField = [newValue retain];
}
}
This was to show himself as a parameter is assigned to deal with their own situation, also looks good like.

This is @ pro ......
So the official version is that this one?

No comments:

Post a Comment