iOS memory management - clarifications

好久不见. 提交于 2020-01-05 07:44:10

问题


I know that alloc and retain will increase the reference count of an object. Is there any other different method that actually increment the reference count? And when/how does dealloc is called?


回答1:


With these the retain count gets increased.

  1. new, however it can be seen as alloc+init.

  2. retain

  3. copy creates new object with retain count=1

  4. mutableCopy creates new object with retain count=1

dealloc is called automatically as soon as retain count reaches to 0.




回答2:


  • alloc allocates an object with retain count 1.
  • Methods that start with new also return an object with retain count 1.
  • retain increments the count by 1.
  • release and autorelease (at the end of the run loop) decrement it by 1.
  • Methods that start with the name of the class (without prefix) return an autoreleased object, meaning that it will be released at the end of the cycle, if you don't retain it yourself.
  • Finally, methods that copy an object (usually start with copy) also create a copy with retain count 1.

dealloc is called when the retain count of an object drops to 0.

PS. In case you didn't know about it yet, consider using Automatic Reference Counting (ARC).



来源:https://stackoverflow.com/questions/14043994/ios-memory-management-clarifications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!