Question about factory method object lifetimes in Objective-C/Cocoa (to retain or not…)

前端 未结 4 1069
遇见更好的自我
遇见更好的自我 2021-01-03 11:38

From reading the memory management docs in the SDK, I gathered that factory methods (static constructor methods) would typically be retaining the object for me and adding it

4条回答
  •  礼貌的吻别
    2021-01-03 11:45

    Usually auto-release pools are drained at the end of the current event loop. A pretty good rule of thumb is that unless you're returning an autoreleased object, if you want that object to remain outside of the current method, you should retain it.

    So, when you create your NSMutableArray as an autoreleased object, once your method ends, all bets are off, and that autorelease pool could drain at any time. Retain it, and then release it in your dealloc.

提交回复
热议问题