Lets suppose I created a property tempStr that is of NSString type.
I synthesized it obviously.
In one of my methods, I set the value of tempstr to be yellowcolor.
Then just after that I reinitialized tempStr with redcolor.
So I wanna know what happens to the memory of tempStr in this case.
Thanx.
It depends on what attribute you set for your property:
retain,assignorcopy.@property (retain) NSString *tempStr: the old value (yellowcolor) would be released and the new value (redcolor) would be retained. The only exception is whenyellowcolor == redcolor. Then nothing would happen, because old and new values are the same.@property (assign) NSString *tempStr: there would be no release/retain operations. It is equal to changing just a pointer. So after this operations yellowcolor won’t be released and you’ll lost a reference to it (if there is no other reference to it in your code). So it can cause a memory leak.@property (copy) NSString *tempStr: it’s similar toretainbut it callcopyon new value instead of justretain, so it’d create a duplicate object in a memory. Considering NSString, it’s equal toretain, because NSString is immutable, so there is no need to make a duplicate.You can find some code examples here.
EDIT: As @Bavarious mentioned,
copyis equal toretainonly if you initialize this property withNSString. It won’t be equal if you initialize it withNSMutableString, because this one is mutable, so the “proper” copy would be make.