简体   繁体   中英

Xcode static analyzer and copyWithZone

The Xcode 4 static analyzer flags this method as a having an over-released return value when that does not seem to be the case.

- (id)copyWithZone:(NSZone *)zone
{
    return [[[self class] allocWithZone:zone] initWithURL:self.url postString:self.postString];
}

There is an arrow pointing from the return keyword to the expression following it, and another from that expression to the analyzer warning. Here is the static analysis:

  1. Method returns an Objective-C object with a +1 retain count
  2. Object sent -autorelease message
  3. Object returned to caller as an owning reference (single retain count transferred to caller)
  4. Object returned to caller with a +0 (non-owning) retain count
  5. Object with +0 retain counts returned to caller where a +1 (owning) retain count is expected

Is the static analyzer incorrect or is there something wrong with this code?


By request, the -initWithURL:postString: method:

- (id)initWithURL:(NSURL *)u postString:(NSString *)p
{
    if ( (self = [super init]) ) 
    {
        self.url = u;
        self.postString = p;
    }
    return self;
}

I continue to get this warning even after cleaning and rebuilding the project.

UPDATE: The Xcode static analyzer no longer flags this as an issue after upgrading to Xcode 4.2.

That's a bug in Xcode. The code is alright.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM