Objective-C autorelease memory management

前端 未结 4 669
粉色の甜心
粉色の甜心 2021-02-07 11:51

Im trying to understand when to call autorelease, and what this will actually do to my object.

After reading About Memory Management in the Mac Developer Library I under

4条回答
  •  一向
    一向 (楼主)
    2021-02-07 12:24

    An autoreleased object is added to an autorelease pool.

    Objects in the autorelease pool are released at the end of an iteration of the main runloop (or sooner if you are managing your own autorelease pool and/or if you call drain).

    When you call a method that returns an autoreleased object, it is pretty much guaranteed to stay valid until at least the end of the scope that it was called in.

    If you want to ensure that it stays alive longer then you should retain it, remembering to release it again when you're finished with it.

提交回复
热议问题