iOS 上避免内存泄漏的最佳方案
我正在构建一个读取 rss 文件(例如 Bing 或 Pulse)的应用程序,因此我构建了一个类(UIViewController),它显示提要中的每个单独条目,以及一个包含该条目列表的类和另一个显示所有条目的类。我没有使用任何 xib 文件,因此我在每个类中实现了一个方法,该方法在每次旋转后被调用,以重置该类内视图的框架。但我在内存中遇到了一个问题,特别是当发生旋转时调用该方法时,知道它只是为视图中的每个帧分配了一个 CGRect 。 那么您能否帮助我避免这个问题或建议一种不同的方法来避免它,我关于不使用 xib 文件是否正确,或者我应该尽可能使用它们并在其中设置旋转内容(使用自动调整大小蒙版) )。
如果有一些复杂的免费示例(例如这些应用程序),任何人都可以向我指出它。
任何帮助将不胜感激, 并提前致谢...
I'm building an application that reads rss files something like Bing or Pulse, so I've built a class (UIViewController) that shows each individual entry in the feed and a class that contains that list of entries and another class that shows all the feeds at once, and I've not used any xib files so I've implemented a method in each class that gets called after each rotation to reset the frames of the views inside that class. But I'm facing a problem in the memory especially when calling that method when a rotation happens knowing that it just assigns a CGRect to each frame in the view.
So could you please help me to avoid this problem or suggest a different way to avoid it, and am I right about not using xib files or should I use them when ever I can and set the rotation thing inside them (using the auto resizing mask).
And if there is some complex free example like those applications, could any body point me to it.
Any help would be greatly appreciated,
and thanks in advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,当 nib 文件对您有用时,没有理由避免它们。它们通常很有用。当 nib 文件对您没有用处时,没有理由使用它们。如果您有复杂的布局,它们绝对非常有用。然而,是否使用它们是基于特定应用的个人选择。作为初学者,我建议使用它们并熟悉它们,特别是依靠 Xcode 模板来正确设置它们。
Nib 文件不是您的问题,无论如何。您正在泄漏内存,您需要调查原因。首先,运行静态分析器以确保没有明显的错误。然后在“仪器(泄漏)”下运行您的应用程序并查找大泄漏。然后在仪器(分配)下运行您的应用程序并查找占用内存最多的内容。这应该指出你的错误。
在不深入了解您的代码的情况下,最可能的原因是您滥用了 ivars。确保通过访问器访问 ivars(
init
、dealloc
和访问器中除外)。直接访问 ivars 是 iOS 内存问题的第一大原因。First, there is no reason to avoid nib files when they are useful to you. They are often useful. There is no reason to use nib files when they are not useful to you. If you have complex layout, they are definitely very useful. Whether to use them or not, however, is a personal choice based on the particular application. As a beginner, I would recommend using them and getting comfortable with them, and particularly relying on the Xcode templates to set them up correctly.
Nib files aren't your issue, one way or another here. You're leaking memory, and you need to investigate why. First, run the static analyzer to make sure there are no obvious errors. Then run your app under Instruments (Leaks) and look for large leaks. Then run your app under Instruments (Allocations) and look for what's eating the most memory. That should point you to your error.
The most likely cause, without any insight into your code, is that you are misusing ivars. Make sure that you access your ivars through accessors (except in
init
,dealloc
and in the accessors). Directly accessing your ivars is the #1 cause of memory problems in iOS.正确释放已全局分配和定义的对象。当 UIViewController 的对象处于活动状态时,不要释放它。大多数泄漏问题是由于在 UIViewController 的对象超出范围之前释放它而发生的。
Release the objects properly which has been allocated and defined globally. Do not release the object of your UIViewController when it is active. Most of the leakage problems occur by releasing the object of the UIViewController before it reaches out of scope.