2

Possible Duplicate:
NSString @property, using copy instead of retain

I have a basic memory management problem as below:

NSString *someName = [NSString stringWithFormat:@"Chris"];
Person *p = [[Person alloc] init];
p.name = someName;

if "name" is set to be "retain", I know that "someName" will be autorelease once and retain once, so "p.name" will keep the NSString with one retainCount.

But how about if "name" is set to be copy. Although "someName" give a copy of itself to "p.name", but the copy one is stil will be autorelease, right? So, do I need to retain "someName" explicitly?

Community
  • 1
  • 1
itenyh
  • 1,921
  • 2
  • 23
  • 38

2 Answers2

1

No, copy does not autoreleased the copy before it returns it (or, to put it another way, it returns an object that you own). Copy properties would be pretty worthless if it did, wouldn't they?

Chuck
  • 234,037
  • 30
  • 302
  • 389
1

You do not need to release someName in either of these cases: copy becomes an independent object; its creation does not increment the original's reference count.

Retaining will increment ref count of someName precisely the way you described in your post.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523