Which iPhone OS memory management rules and how-to's do you know?

后端 未结 4 1203
臣服心动
臣服心动 2021-02-09 23:03

Currently I am jumping into the ice cold water called \"memory management in iPhone OS\".

Here\'s one rule i\'ve learned:

Every time I see an alloc in my method,

4条回答
  •  心在旅途
    2021-02-09 23:41

    I tend to create only autoreleased objects, either by using a class method or by autoreleasing it immediately after creation, unless I can state a reason not to. For example:

    • I am assigning it to a member variable because I intend to hold onto it for a while.
    • I am only creating it to pass it on immediately to another method, and I send it a release message right after that method call.
    • For performance reasons, I need to free that memory before the nearest NSAutoreleasePool will be released, such as creating a large number of objects inside a loop or the objects are holding onto a large amount of data (e.g., images).

    That way, I am less likely to leak objects. By default, I create them autoreleased, and when I make the deliberate decision not to autorelease them, I am immediately faced with the question of where they will be released.

    For object properties, rather than releasing them in my dealloc method, I like to assign nil to them. That way, retained or copied properties are sent a release, while assigned properties are simply overwritten, and I don't have to update my dealloc method if I change the property to/from retained.

提交回复
热议问题