0

Hi i am Android developer and recently i moved to i-phone developing and i am present learning about memory management,I read so many documents about memory management but i have little confusion about below statement what happen when we use retain and copy and strong and weak for below statement's

code1:-

NSString* name = [[NSString alloc]init];--->retain count 1

[name retain]

[name copy]

[name release]

code2:-

@propery(nonautomic,strong)NSString*name;
@propery(nonautomic,weak)NSString*name;
@propery(nonautomic,retain)NSString*name;
@propery(nonautomic,copy)NSString*name;
Krish
  • 4,166
  • 11
  • 58
  • 110
  • when you call retain , retain count increase by 1. When you copy it, the retain count stays same. When you release it, the retain count decrease by 1. – Teja Nandamuri Jul 17 '17 at 15:02
  • then what is diff b/w retain and copy? – Krish Jul 17 '17 at 15:03
  • As I said, retain transfers the ownership of the object, if you assign [name retain] to another object B, then you are transferring the ownership of name to B, in this case you need to control the memory retain count for B instead of name. IF you use copy, then you are not transferring the ownership, you still need to control the retain count for name! – Teja Nandamuri Jul 17 '17 at 15:05
  • can u exaplain with some example please – Krish Jul 17 '17 at 15:07
  • pls refer to https://stackoverflow.com/questions/4510913/objective-c-assign-copy-retain – Teja Nandamuri Jul 17 '17 at 15:13
  • And [NSString property: copy or retain?](https://stackoverflow.com/questions/387959/nsstring-property-copy-or-retain). – Willeke Jul 17 '17 at 15:15
  • `retain`does not transfer the ownership, but adds an ownership. @TejaNandamuri – Amin Negm-Awad Jul 17 '17 at 15:30
  • thanks for clarifying , then what transfers the ownership ? @AminNegm-Awad – Teja Nandamuri Jul 17 '17 at 15:30
  • 1
    In MRR you can "transfer" the ownership by retaining the new reference (=adding an ownership) *plus* releasing the old one (relinquish an ownership), what dos obviously nothing over all. However, there is no reason to look at it this way, because every reference should be viewed separately. (Maybe you got confused with bridging.) – Amin Negm-Awad Jul 17 '17 at 15:33
  • Also, please use "iPhone". and not "i-phone". Thanks. – GeneCode Jul 18 '17 at 03:07

0 Answers0