避免,查找和删除可可中的内存泄漏 [英] Avoiding, finding and removing memory leaks in Cocoa

查看:111
本文介绍了避免,查找和删除可可中的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发生内存(和资源)泄漏。如何确保他们不会?



什么提示和放大器?技术,你会建议帮助避免在一开始创建内存泄漏?



一旦你有一个泄漏的应用程序,你如何跟踪泄漏的源头?



(哦,请避免使用just use GC的答案。直到iPhone支持GC这不是一个有效的答案,甚至可能泄漏资源和内存on GC)

解决方案

在XCode 4.5中,使用内置的静态分析器



在版本XCode之前的3.3,你可能需要下载静态分析器。这些链接告诉您如何操作:



使用LLVM / Clang静态分析器



首先,使用 Clang静态分析器 - 不出奇地 - 分析您的C和Objective-C代码(没有C + +)在Mac OS X 10.5。安装和使用非常简单:


  1. 此页面

  2. 从命令行, cd

  3. $ b
  4. 执行 scan-build -k -V xcodebuild
    $ b

    (还有一些额外的约束等,特别是你应该在它的Debug配置中分析一个项目 - 参见 http://clang.llvm.org/StaticAnalysisUsage.html 了解详细信息 - 但这是更多或更少,它归结为。)



    然后,分析器会为您生成一组网页,显示内存管理和编译器无法检测的其他基本问题。



    如果您的项目没有定位到Mac OS X桌面,还有以下几个细节:


    1. 所有配置到使用Mac OS X桌面框架的SDK ...

    2. 将命令行构建设置为使用调试配置。

    (这大体上与这个问题。)


    Memory (and resource) leaks happen. How do you make sure they don't?

    What tips & techniques would you suggest to help avoid creating memory leaks in first place?

    Once you have an application that is leaking how do you track down the source of leaks?

    (Oh and please avoid the "just use GC" answer. Until the iPhone supports GC this isn't a valid answer, and even then - it is possible to leak resources and memory on GC)

    解决方案

    In XCode 4.5, use the built in Static Analyzer.

    In versions of XCode prior to 3.3, you might have to download the static analyzer. These links show you how:

    Use the LLVM/Clang Static Analyzer

    To avoid creating memory leaks in the first place, use the Clang Static Analyzer to -- unsurprisingly -- analyse your C and Objective-C code (no C++ yet) on Mac OS X 10.5. It's trivial to install and use:

    1. Download the latest version from this page.
    2. From the command-line, cd to your project directory.
    3. Execute scan-build -k -V xcodebuild.

    (There are some additional constraints etc., in particular you should analyze a project in its "Debug" configuration -- see http://clang.llvm.org/StaticAnalysisUsage.html for details -- the but that's more-or-less what it boils down to.)

    The analyser then produces a set of web pages for you that shows likely memory management and other basic problems that the compiler is unable to detect.

    If your project does not target Mac OS X desktop, there are a couple of other details:

    1. Set the Base SDK for All Configurations to an SDK that uses the Mac OS X desktop frameworks...
    2. Set the Command Line Build to use the Debug configuration.

    (This is largely the same answer as to this question.)

    这篇关于避免,查找和删除可可中的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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