为什么在Objective-C中,在单独的语句中执行alloc和init会导致根据Xcode静态分析器释放对象? [英] Why in Objective-C does doing alloc and init in separate statements cause the object to be released according to the Xcode static analyzer?

查看:76
本文介绍了为什么在Objective-C中,在单独的语句中执行alloc和init会导致根据Xcode静态分析器释放对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Objective-C迈出第一步,尽管遇到了一个小问题,但XCode 4.1中的静态分析器(Product-> Analyze)令人困惑.我为有理数创建了一个简单的Fraction类,可以像这样分配和初始化

I'm making my first steps with Objective-C and have encountered a minor, albeit confusing issue with the static analyzer (Product->Analyze) in XCode 4.1. I have created a simple Fraction class for rational numbers which I allocate and initialize like this,

Fraction* f = [[[ Fraction alloc ] initWithNumerator:3 withDenomimator:5]
                                                               autorelease];
[ f print ];

其中print是仅使用NSLog来显示分数的方法,一切正常.但是,如果我将alloc/init构造拆分成两个语句(我意识到这是非惯常的-我只是想了解机器),而是使用手动release而不是autorelease给出:

where print is a method which just uses NSLog to display the fraction, everything works fine. However, if I split the alloc/init construct apart into two statements (I realize this is non-idiomatic - I'm just trying to understand the machinery) and use manual release rather than autorelease giving:

Fraction* f = [ Fraction alloc ]; // 1. Method returns an Objective-C object with
                                  //    a +1 retain count (owning reference)
[ f initWithNumerator:3 withDenomimator:5]; // 2. Object released
[ f print ]; // 3. Reference-counted object is used after it is released
[ f release ];

该程序似乎仍然可以正常运行,但是XCode分析器在注释中给出了警告.为什么XCode认为init调用导致对象被释放?

the program still appears to run without error, but the XCode analyzer gives the warnings in the comments. Why does XCode think the init call causes the object to be released?

进一步思考这个问题时,我看到我的两个程序并不完全等效,因为在第一个代码段中,我的指针f是对init的调用的结果,而在第二个代码段中,则是alloc的结果.因此,将我的代码更改为

Thinking through this further as I frame the question I can see that my two programs are not quite equivalent because in the first snippet my pointer f is the result of the call to init whereas in the second snippet it is the result of alloc. So changing my code to,

 Fraction* a = [ Fraction alloc ];
 Fraction* f = [ a initWithNumerator:3 withDenomimator:5];
[ f print ];
[ f release ]; // or should it be [ a release ] ?

使其完全等效,并且静态分析器停止抱怨.那么init是否有可能返回与从alloc传递给它的指针不同的指针,而不仅仅是返回配置已传递的内存?通过此代码,我应该将[ a release ]alloc配对还是将[ f release ]init配对?

makes it exactly equivalent and the static analyzer stops complaining. So is it possible that init can return a different pointer than the one passed to it from alloc, rather than just configuring the memory it has been passed? With this code should I pair [ a release ] with the alloc or [ f release ] with the init?

推荐答案

那么,有可能init可以返回与从alloc传递给它的指针不同的指针,而不仅仅是配置已传递的内存吗?

So is it possible that init can return a different pointer than the one passed to it from alloc, rather than just configuring the memory it has been passed?

绝对.

使用此代码,我应该将[release]与alloc配对还是[f release]与init配对?

With this code should I pair [ a release ] with the alloc or [ f release ] with the init?

,您可以将初始化对象的值分配给f(如您所愿).此时,a可能是一个悬空指针(如果返回了另一个地址).因此,f应该为release d.

you would assign the value of the initialized object to f (as you have). at this point, a may be a dangling pointer (if another address is returned). thereore, f should be released.

对此顺序的解释是,对象可能选择返回其自身的特殊版本/变体,并且此重新分配沿init...链进行.

the explanation for this order is that the object may have opted to return a specialized version/variant of itself, and this reallocation happens along the init... chain.

愚蠢的示范

@interface Fraction : NSObject
{
@private
    int numerator;
    int denominator;
}

@end

static Fraction* EvilFraction = ...;

@implementation Fraction

- (id)initWithNumerator:(int)num denominator:(int)den
{
    self = [super init];
    if (nil != self) {
        if (0 == den){
            [self release];
            return [EvilFraction retain];
        }
    }
    return self;
}

@end

这篇关于为什么在Objective-C中,在单独的语句中执行alloc和init会导致根据Xcode静态分析器释放对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆