【问题标题】:Incorrect decrement of the reference count of an object that is not owned at this point by the caller For retain property调用者此时不拥有的对象的引用计数的不正确递减对于保留属性
【发布时间】:2013-06-20 20:06:00
【问题描述】:

我创建了一个具有保留属性的 NSString fName 属性,并合成了该属性。 我在 viewDidLoad 上初始化了该属性。

我的实际问题是,我使用了 [self.fName release]。此示例工作正常,但静态分析器将此行显示为错误“调用者此时不拥有的对象的引用计数不正确递减”。

参考代码:

@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

【问题讨论】:

    标签: ios iphone objective-c memory-management


    【解决方案1】:

    那里不需要释放。你不分配/初始化任何东西。如果你正在做这样的事情:

    self.fName = [[NSString alloc] initWithString:@"Hello"]; 
    

    那么你必须释放 self.fName。

    根据经验,numberOfReleases = numberOfAlloc。

    现在,黄金法则,使用 ARC :)

    【讨论】:

    • 但是属性属性是保留。当我们为此属性设置一些东西时,它应该增加保留计数吗?这是正确的吗?请澄清丹尼帕塔先生。
    • 是的,如果您将一个已经是 alloc/init(保留计数至少 1)的新对象设置为“保留”属性,则该对象的保留计数会增加,但不是你的情况。
    • self.fName = [[NSString alloc] initWithString:@"Hello"];我更新了这个。你能解释一下为什么在我的情况下它没有增加保留计数..请帮忙。
    猜你喜欢
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多