How can I fix this clang warning: "Object with +0 retain counts returned to caller where +1 (owning) retain count is expected"? How can I fix this clang warning: "Object with +0 retain counts returned to caller where +1 (owning) retain count is expected"? objective-c objective-c

How can I fix this clang warning: "Object with +0 retain counts returned to caller where +1 (owning) retain count is expected"?


I suspect it's just noticing a method with the prefix copy and flagging that as something that should return something that the caller owns, because it thinks it's following Cocoa naming conventions.

In your case, of course, you are referring to files and whatnot, so it's an ignorable warning. If you change the name of your method, to something like saveData: instead, I bet the warning will go away.


Also, for times where you really do want to name a method with 'copy' or something because regardless of Cocoa memory management guidelines, copy is the best name for the method, you can annotate the method declairation with NS_RETURNS_NOT_RETAINED and then Clang won't give you a warning. So:

// Copies data from data to string; does not follow the copy rule- (NSString*)copyData:(NSData*)data NS_RETURNS_NOT_RETAINED;


Since the method has the name copy in it, the analyzer is expecting the returned object to have a +1 retain count, according to the Memory Management Guide.