Hey guys, I'm starting to play around with Objective-C and I want to make sure I get memory/properties right.
Suppose the following code:
@interface Rectangle : NSObject
{
Vector2* origin;
//[...]
}
Rectangle* myRect = [[Rectangle alloc] init];
myRect.origin.x = 100.0f;
[myRect print];
myRect.origin = [[Vector2 alloc] init]; //hummm.. 2 concerns here.
Concern 1:
Suppose origin is a standard (assign) synthesized property:
Does myRect's previous origin ref count goes to 0 automatically when assigning the new Vector2 and GC will take care of it later on? Or I have to call release explicitly inside the property?
Concern 2:
Suppose origin would be a 'retain' property: (BTW: Would that kind of code be automatically generated when declaring a synthesized retain property, is that possible?)
-(void) setOrigin: (Vector2*)newOrigin {
[newOrigin retain];
[origin release]
origin = newOrigin;
}
Then when doing:
myRect.origin = [[Vector2 alloc] init]
Wouldn't that cause a double ref count increment and then needing release to be called twice to avoid leak? Do you guys rely on well-documented code (to know it's a retain property) when using libraries/other people's code to avoid such problems, or is there some safer ways of alloc/init objects?
Thanks for tips!