Ios 不正确地减少了对象的引用计数,该对象此时不属于retain属性的调用方
我创建了一个带有retain属性的NSString fName属性,并合成了该属性。 我在viewDidLoad上初始化了该属性 我的实际问题是,我使用了[self.fName release]。此示例工作正常,但静态分析器将此行显示为错误“调用方此时不拥有的对象的引用计数减少不正确” 参考代码:Ios 不正确地减少了对象的引用计数,该对象此时不属于retain属性的调用方,ios,iphone,objective-c,memory-management,Ios,Iphone,Objective C,Memory Management,我创建了一个带有retain属性的NSString fName属性,并合成了该属性。 我在viewDidLoad上初始化了该属性 我的实际问题是,我使用了[self.fName release]。此示例工作正常,但静态分析器将此行显示为错误“调用方此时不拥有的对象的引用计数减少不正确” 参考代码: @interface ViewController : UIViewController @property(nonatomic,retain)NSString *fName; @end @im
@interface ViewController : UIViewController
@property(nonatomic,retain)NSString *fName;
@end
@implementation ViewController
@synthesize fName;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.fName =@"Hello";
[self.fName release];//Analyzer showgin error here.
}
---------
------
end
没有必要在那里释放。你没有分配/初始化任何东西。如果您正在做这样的事情:
self.fName = [[NSString alloc] initWithString:@"Hello"];
然后您必须释放self.fName
根据经验,numberOfReleases=numberOfAlloc
现在,黄金法则是使用ARC:)但是属性是Retain。当我们设置此属性时,它应该增加保留计数?对吗?请澄清danypata先生。是的,如果您将一个已经是alloc/init(retain count至少为1)的新对象设置为“retain”属性,则该对象的retain count将增加,但这不是您的情况。self.fName=[[NSString alloc]initWithString:@“Hello”];我更新了这个。你能解释一下为什么在我的情况下不增加保留计数吗。。请帮忙。