您知道哪些iPhone OS内存管理规则和操作方法? [英] Which iPhone OS memory management rules and how-to's do you know?

查看:101
本文介绍了您知道哪些iPhone OS内存管理规则和操作方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在进入名为iPhone OS中的内存管理的冰冷水中。

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

这是我学到的一条规则:

Here's one rule i've learned:

每当我在方法中看到一个alloc时,我会在方法的底部发布相应的变量。

Every time I see an alloc in my method, I will release that corresponding variable at the bottom of the method.

每次我在我的头文件中创建一个@property(...),表示复制或保留,我将该变量的释放消息放入dealloc方法。

Every time I create an @property(...) in my header file which says copy or retain, I put a release message on that variable into the dealloc method.

每次我有一个IBOutlet,我做同样的事情。唯一的例外:如果IBOutlet具有@property(... assign)之类的东西,或者换句话说:如果它具有assign关键字。然后我不关心在dealloc方法中释放它。

Every time I have an IBOutlet, I do the same thing. Only exception: If the IBOutlet has something like @property(... assign), or in other words: If it has the assign keyword at all. Then I don't care about releasing it in the dealloc method.

我觉得还有很多好的规则要知道!记下你所拥有的东西。让我们一起刮掉它们。也欢迎链接到精彩的描述。

I feel that there are many more good rules to know! Just write down what you have. Let's scrape them all together. Links to great descriptions are welcome, too.

推荐答案

我使用的规则


  • 使用名称以alloc或new开头或包含copy的方法释放您创建的所有对象。

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

释放您保留的所有对象。

Release all objects you retain.

不要释放使用+ className便利构造函数创建的对象。 (该类创建它并负责释放它。)

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

不要以其他方式释放您收到的对象。
mySprockets = [widget sprockets];

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.

一些好的链接:


  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

  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

这篇关于您知道哪些iPhone OS内存管理规则和操作方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆