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

后端 未结 4 1197
臣服心动
臣服心动 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-09 23:54

    The rules I use

    • Release all objects you create using a method whose name begins "alloc" or "new" or contains "copy".

    • Release all objects you retain.

    • Do not release objects created using a +className convenience constructor. (The class creates it and is responsible for releasing it.)

    • Do not release objects you receive in other ways E.g. mySprockets = [widget sprockets];

    • If you store an object you receive in an instance variable, retain it or copy it. (Unless it's a weak reference - just a pointer to another object, usually to avoid cyclical references.)

    • Received objects are valid within the method they are received in (generally) and are also valid if passed back to the invoker.

    Some good links:

    1. http://www.gehacktes.net/2009/02/iphone-programming-part-2-objective-c-memory-management/
    2. http://mauvilasoftware.com/iphone_software_development/2008/01/iphone-memory-management-a-bri.html

提交回复
热议问题