1

I have a class subclassing UIView called BizView. and I have the following code

@property (nonatomic, retain)BizView * bizPlace;

/

@synthesize bizPlace = _bizPlace;

    -(void) showBiz
    {

        BizView * biz = [[BizView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 30.0)];
        self.bizPlace = biz;
        [biz release];

        self.bizPlace.delegate = self;
        [self.bizPlace doStuff];
    }

    -(void) removeBizView
    {
        [self.bizPlace removeFromSuperview];

        self.bizPlace = nil; //****** this does not call the dealloc of bizPlace
        // [_bizPlace release];   >>>>>>this does call the dealloc

        [self performSelector:@selector(showBiz) withObject:nil afterDelay:1];
    }

    //biz view delegates
    -(void)didBizStuff:(BizView *)bView
    {
        [self.view addSubview:_bizPlace];

        [self performSelector:@selector(removeBizView) withObject:nil afterDelay:1];

    }

    -(void)dealloc
    {
        self.bizPlace = nil;
        [super dealloc];
    }

Now according to the docs and various articles on the internet, setting self.bizPlace = nil should call the dealloc of bizPlace but this is not the case here. However, calling [_bizPlace release] does. What could be the reason for this?

4

1 回答 1

0

因此,您正在比较的两个步骤存在明显差异。当您使用强指针综合 bizPlace = _bizPlace 时,您正在为这个特定元素分配内存。此时,您有一个指针 (_bizPlace) 作为此类的一部分强分配。通过综合设置器将此设置为 nil 不会导致类释放其成员变量,但是在该特定变量上调用 release 会导致 _bizPlace 被释放。

这里发生了不同级别的 nil 设置,这是您应该从中删除的主要内容。您应该注意,综合到变量中的强属性的行为与局部变量分配的行为不同。这是由于指向内存块的人数众多。

于 2012-12-18T04:13:55.923 回答