我有以下内容(改编自Apple低级文件管理 - 解析别名)
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
我是否有内存泄漏?
如果是,我该如何解决?
如果不是,我该如何阻止警告?
答案 0 :(得分:1)
只是提醒您创建的分配给resolvedPath
的对象返回retain count of 1
,因此,除非您的方法以new alloc
或contains copy
开头, caller
无法知道它正在处理retained
对象,因此永远不会得到released
。
要解决此问题,请将您的方法名称从getTarget
更改为newTarget
。
答案 1 :(得分:0)
我是否有内存泄漏?
你应该认为这是泄漏。在GC中,不保证在收集器中注册cf类型的分配。 GC仅涵盖一系列类型(显式objc对象)。您应该假设您创建或复制的cf类型不已在收集器中注册。是否从cf函数中返回了一个cftype,该cftype函数未向收集器注册(不是因为ns类型是cf类型)。
如果是,我该如何解决?
使用CFMakeCollectable或NSMakeCollectable