Objective-C autorelease memory management

前端 未结 4 666
粉色の甜心
粉色の甜心 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.

    0 讨论(0)
  • 2021-02-07 12:26

    The one responsible for creating it and the one who calls runIt has to retain the object. When the one responsible is done with it, it should then release the object.

    0 讨论(0)
  • 2021-02-07 12:36

    Its better to retain the object and release it after the use.

    0 讨论(0)
  • 2021-02-07 12:40

    How can i safely use the returned autoreleased object inside my runIt method if i dont know when the autorelease trigger?

    Autorelease will trigger after the current run loop ends.

    Should i retain the object returned by the createNewTest ? or can i safely use it within the runIt scope?

    You can safely use it inside the runIt scope.

    0 讨论(0)
提交回复
热议问题