cocoa - 核心基础对象 - 潜在泄漏

标签 cocoa garbage-collection core-foundation

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

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

构建和分析会生成以下警告:-

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

我有内存泄漏吗?

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

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

最佳答案

它只是提醒您,创建的分配给 resolvedPath 的对象返回时,保留计数为 1,因此除非您的方法以 开头new alloccontains copy调用者无法知道它正在处理一个retained对象,因此它将永远不会被释放

要修复此问题,请将方法名称从 getTarget 更改为 newTarget

关于cocoa - 核心基础对象 - 潜在泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7377029/

相关文章:

objective-c - 如何在 NSOutlineView 中自动设置行高以适合文本?

c# - 当垃圾收集器在堆中移动数据时,引用是否会更新?

java - G1GC 非常高的 GC 计数和 CPU,非常频繁的 GC 会降低性能

ios - __bridge_transfer 在 NULL 对象上是否有效

objective-c - 这些 if(0) 条件句有什么意义?

objective-c - NSImage 绘制矩形 :fromRect:operation:fraction results in a flipped image

cocoa - 何时创建自定义 NSNotificationCenter?

cocoa - 使文档窗口居中

swift - 如何在 Swift 中比较 CGError

java - 如何使列表中的对象符合垃圾收集条件?