An object with +0 counters returned to the caller where +1 is expected (ownership)

I have a set of classes that were created by www.sudzc.com (an awesome WDSL web service proxy tool for iPhone / Flex / Javascript).

When I run CMD + SHIFT + A to check for memory leaks, I get the following message:

An object with a return value of +0 for the caller, where +1 (owning) is expected to be counted

Here is the method by which it returns this message for:

// Static method for initializing from a node. + (id) newWithNode: (CXMLNode*) node { return (id)[[[SoapObject alloc] initWithNode: node] autorelease]; } 

I do not want to receive messages with this code, and it will need to be regenerated many times through the project, since changes to web services are changing, and I need to update proxy classes.

Any ideas?

Thanks in advance.

Jason

+6
memory-leaks iphone
source share
3 answers

The analyzer complains because the memory management guide determines that ...

You "create" an object using a method whose name begins with "alloc" or "new" or contains "copy".

Cocoa and Objective-C rely heavily on the agreement, you should do your best to follow this. Turn on “treat warnings as errors” and fix the problem. Although you may be the only person working on it now, if at some point another developer was supposed to use your methods, they will probably follow the memory management guidelines and ultimately re-issue the object returned by this method (and the application crashes) .

+12
source share

The method is marked because the method name has the prefix 'new'. The static analyzer simply comments that, using the usual method naming conventions, one would expect this method to return the object you are about to release, rather than an object with auto-implementation.

A “regular” naming convention for methods such as a method prefix with a class name, for example, if this method was defined for the Widget class:

 @interface Widget : NSObject { } + (id)widgetWithNode:(CXMLNode*)node; // Returns an object that has been autoreleased. - (id)initWithNode:(CXMLNode*)node; // Returns an object you are expected to release. @end 

If you use the method correctly (that is, you take into account the fact that it returns an object with auto-implementation), you can simply ignore this warning.

+5
source share

If you have a method name that should have something like “new” or “copy” in it, and you know that the warning is not valid, you can exclude this warning by including in the LLVM a hint that the class is really in order ,

In your header file, first add this (usually near the top, but it could be anywhere):

 #ifndef __has_feature // Optional. #define __has_feature(x) 0 // Compatibility with non-clang compilers. #endif #ifndef NS_RETURNS_NOT_RETAINED #if __has_feature(attribute_ns_returns_not_retained) #define NS_RETURNS_NOT_RETAINED __attribute__((ns_returns_not_retained)) #else #define NS_RETURNS_NOT_RETAINED #endif #endif 

Then at the end of your method declaration add this:

 + (id) newWithNode: (CXMLNode*) node NS_RETURNS_NOT_RETAINED; 

You can find a list of other tips (really attributes) that you can pass to LLVM here:

http://clang-analyzer.llvm.org/annotations.html

0
source share

All Articles