【问题标题】:Core Foundation object - Potential leak核心基金会对象 - 潜在泄漏
【发布时间】:2011-09-11 08:02:32
【问题描述】:

我有以下(改编自 Apple Low Level File Management - Resolving Aliases)

NSString *resolvedPath = nil;
...
resolvedPath = (NSString*)CFURLCopyFileSystemPath(resolvedUrl, kCFURLPOSIXPathStyle);
...
return resolvedPath;

Build & Analyze 生成以下警告:-

194:3 Potential leak (when using garbage collection) of an object allocated on line 187 and stored into 'resolvedPath'

详细说明:-

187:32 Call to function 'CFURLCopyFileSystemPath' returns a Core Foundation object with a +1 retain count (owning reference).  Core Foundation objects are not automatically garbage collected

194:3 Object returned to caller as an owning reference (single retain count transferred to caller)

194:3 Object allocated on line 187 and stored into 'resolvedPath' and returned from method 'getTarget:' is potentially leaked when using garbage collection.  Callers of this method do not expect a returned object with a +1 retain count since they expect the object to be managed by the garbage collector

我有内存泄漏吗?

如果是这样,我该如何解决?

如果不是,我该如何防止出现警告?

【问题讨论】:

    标签: cocoa garbage-collection core-foundation


    【解决方案1】:

    它只是提醒您创建的分配给resolvedPath 的对象返回retain count of 1,因此除非您的方法以new alloc 或contains copy 开头,否则caller 无法知道它正在处理一个retained 对象,因此它永远不会得到released。

    要修复它,请将您的方法名称从 getTarget 更改为 newTarget。

    【讨论】:

      【解决方案2】:

      我有内存泄漏吗?

      你应该假设它是一个泄漏。在 GC 中,不保证 cf 类型的分配会向收集器注册。 GC 仅涵盖一系列类型(显式 objc 对象)。您应该假设您创建或复制的 cf 类型未在收集器中注册。未公开是否从已向收集器注册或未注册的 cf 函数返回 cftype(可能是因为 ns 类型是 cf 类型)。

      如果是这样,我该如何解决?

      使用 CFMakeCollectable 或 NSMakeCollectable

      【讨论】:

      • 谢谢,NSMakeCollectable 似乎已经完成了这项工作。可惜在文档中很难找到这些概念,但是一旦找到就很清楚了。
      猜你喜欢
      • 2011-04-15
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多