【问题标题】:Xcode static analyzer performance when using singletons使用单例时的 Xcode 静态分析器性能
【发布时间】:2011-07-15 21:49:03
【问题描述】:

我无法理解 XCode 中的静态分析器如何识别涉及单例的可能泄漏。在下面的代码中,我收到警告,第一行以及第一个 return 语句中可能存在泄漏。分析器将这些警告放在第二个 return 语句中。由于我没有在这些行中进行任何分配,因此我认为是对单例的访问及其可能的分配导致了此警告。谁能向我解释一下静态分析器发生了什么?

        if ((self.tableView.editing & ([[[[GroupList sharedGroupList] newGroup] linkArray] count] < [[GrazeConstants sharedGrazeConstants] maxLinksPerGroup])) | 
            (([[[[GroupList sharedGroupList] newGroup] linkArray] count] == 0) & !self.tableView.editing)) {
            return ([[[[GroupList sharedGroupList] newGroup] linkArray] count]+1);
        } else {
            return ([[[[GroupList sharedGroupList] newGroup] linkArray] count]);
        }

【问题讨论】:

    标签: ios xcode cocoa-touch cocoa


    【解决方案1】:

    new-prefixed 方法等价于 alloc+init 根据 Cocoa 内存管理约定对。静态分析器假设您的 newGroup 方法返回一个保留计数为 +1 的对象,而不是自动释放的对象。

    重命名方法(例如,groupmakeGroup),如果无法更改名称,请添加 NS_RETURNS_NOT_RETAINED attribute

    【讨论】:

    • 谢谢。我注意到似乎与新的前缀方法存在相关性。但我从来没有想过分析器会使用名称来假设行为。很容易解决。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多