Dealing with memory on the iPhone is one of the most difficult
challenges that you will face as a new iPhone developer. Today,
I am going to show you a very simple tip that will help you
debug the memory problems that you are likely to face.
One thing that you have to worry about with memory management is
the “retain count” of an object. The retain count is how the
system keeps track of the memory used by an object. If an
object’s retain count is zero and your attempt to access it
your app will crash; if you do not make sure the retain
count of your object is 0 when the object goes out of focus you
will have a memory leak.
Clearly, it is important to be able to find out what the current
retain count of an object is. Here is how you can do this:
NSLog([NSString stringWithFormat:@"Retain Count:%i", [someObject retainCount]]);
This writes out the current retain count to the log. The
important function is [someObject retainCount].
That is it - know how to find out the retain count of a particular object will give you some help in the fight against randomly crashing (or leaky) apps.
本文介绍了一个简单的技巧,帮助iPhone开发者解决应用中常见的内存管理问题。通过监控对象的引用计数,可以有效避免程序崩溃和内存泄漏。

被折叠的 条评论
为什么被折叠?



