What is IOS retain do at backend [closed]

2019-08-18 06:18发布

Consider I am allocating an object. Please see the below image

enter image description here

In the above image *myObject is a pointer. It is referring to an space allocated to that object. When we retain the object, I like to know what it does at backend. Also I like to know why we are doing that.

Also when we release it how it works at backend.

I also like to know what happens at backend when we copy an object.

I am really sorry if my question is so basic.

1条回答
2楼-- · 2019-08-18 07:06

What retain actually does under the hood is:

  1. Take a global lock in the objc runtime
  2. Look up a hash table entry based on the pointer to the object being retained
  3. Increment a counter in that entry
  4. Release the lock

I don't expect this to be all that helpful in practice, but I think it's what you were asking for.

查看更多
登录 后发表回答