微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

objective-c – 调用方此时不拥有的对象的引用计数的不正确递减

我有一个非常简单的Person类,它有一个叫做名字的ivar(一个Nsstring).当我尝试在dealloc中释放这个ivar时,静态分析器给我一个奇怪的错误

Incorrect decrement of the reference
count of an object that is not owned
at this point by the caller

我究竟做错了什么?

顺便说一句,这是我的代码

@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

解决方法

您正在释放从属性getter方法返回的对象,在许多情况下,这将指示可能的错误.这就是静态分析正在捡起它的原因.

相反,使用:

self.name = nil;

要么:

[name release];
name = nil;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐