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

后端 未结 4 1199
臣服心动
臣服心动 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.

    0 讨论(0)
  • 2021-02-09 23:43

    Memory management can seem daunting when you're seeing segfaults spring from every seeming innocent line of code, but it's actually pretty easy once you get the hang of it. Spend a little time reading this page and then Apple's documentation, and you should be writing bug-free code in no time.

    0 讨论(0)
  • 2021-02-09 23:54

    Actually, any time you initialize an object and the method name includes "init" you are responsible for releasing it. If you create an object using a Class method that does not include the word "init" then you don't.

    For example:

      NSString *person = [NSString stringWithFormat:"My name is %@", name];
    

    does not need a release. But:

      Person *person = [[Person alloc] init];
    

    needs a release (as you stated in your question). Likewise:

      Person *person = [[Person alloc] initWithName:@"Matt"]];
    

    also needs a release.

    This is a convention, not a rule of the language, but you will find that it is true for all Apple-supplied APIs.

    0 讨论(0)
  • 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
    0 讨论(0)
提交回复
热议问题