我有以下内容(改编自 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 alloc
或 contains copy
,调用者
无法知道它正在处理一个retained
对象,因此它将永远不会被释放
。
要修复此问题,请将方法名称从 getTarget
更改为 newTarget
。
关于cocoa - 核心基础对象 - 潜在泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7377029/