Incorrect decrement of reference counting of an object that is not currently owned by the caller

I have a very simple Person class that has ivar named name (NSString). When I try to release this ivar in dealloc, the static analyzer gives me a strange error:

Incorrect link decrement counting an object that is not currently owned by the caller

What am I doing wrong?

By the way, here is my code:

@interface Person : NSObject { } @property (copy) NSString *name; @property float expectedRaise; @end @implementation Person @synthesize name, expectedRaise; -(id) init { if ([super init]) { [self setName:@"Joe Doe"]; [self setExpectedRaise:5.0]; return self; }else { return nil; } } -(void) dealloc{ [[self name] release]; // here is where I get the error [super dealloc]; } @end 
+4
source share
1 answer

You are releasing an object returned from the getter method of the property, which in many cases would be a sign of a possible error. This is why static analysis picks it up.

Use instead:

 self.name = nil; 

or

 [name release]; name = nil; 
+18
source

All Articles