开发者社区> 问答> 正文

关于iphone-应用报错incorrect decrement 的问题

创建NSString属性,并且合成属性,在viewDidLoad中对属性进行初始化。

问题:使用 [self.fName release],静态分析其显示错误
`'Incorrect decrement of the reference count of an object that is not owned at this point by the caller'.
`
相关代码:

@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

展开
收起
爵霸 2016-03-19 10:14:58 2150 0
1 条回答
写回答
取消 提交回答
  • 对于控制器中的引用类型的释放,我们通常会把它放到- (void)dealloc -(void)viewDidUnLoad 中来处理,而不是在使用的过程中。在使用的过程中去释放对象可能导致其它地方对该对象的引用无效,致使在访问相关对象时导致程序崩溃。 常用的做法是

    -(void)viewDidUnLoad {
        self.fName=nil;
        [super viewDidUnLoad];
    }
    
    -(void)dealloc {
      [_fName release];
      [super dealloc];
    }
    2019-07-17 19:07:30
    赞同 展开评论 打赏
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载