I have the following (adapted from Apple Low Level File Management - Resolving Aliases)
NSString *resolvedPath = nil;
...
resolvedPath = (NSString*)CFURLCopyFileSystemPath(resolvedUrl, kCFURLPOSI开发者_运维百科XPathStyle);
...
return resolvedPath;
Build & Analyse generates the following warning:-
194:3 Potential leak (when using garbage collection) of an object allocated on line 187 and stored into 'resolvedPath'
The detail states:-
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
Do I have a memory leak?
If so how do I fix it?
If not how do I prevent the warnings?
It is just alerting you that the object created that is assigned to resolvedPath
is returned with a retain count of 1
, therefore unless your method is starting with new alloc
or contains copy
, the caller
has no way to know it is dealing with a retained
object, and it will therefore never get released
.
To fix it change your method name from getTarget
to newTarget
.
Do I have a memory leak?
you should assume it is a leak. in GC, the allocation of a cf type is not guaranteed to be registered with the collector. GC only covers a range of types (explicit objc objects). you should assume cf types which you create or copy are not registered with the collector. whether you are returned a cftype from a cf function that is or is not registered with the collector is not disclosed (it may be because an ns type is a cf type).
If so how do I fix it?
using CFMakeCollectable or NSMakeCollectable
精彩评论