Objective-C-弱属性-吸气剂自动释放(自动引用计数) [英] Objective-C - weak property - getter autoreleases (Automatic Reference Counting)

查看:127
本文介绍了Objective-C-弱属性-吸气剂自动释放(自动引用计数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对ARC中的weak属性(自动引用计数)有疑问

I have a doubt regarding weak property in ARC (auto reference counting)

我的理解(如果我做错了,请纠正我):

weak属性的行为与assign属性类似,不同之处在于,当该属性指向的实例被销毁时,使ivar指向nil.

weak property behaves similar to the assign property except that when the instance that the property was pointing to gets destroyed, the ivar is made to point to nil.

问题:

  1. 我只是觉得weak属性的getter会保留并自动释放.是不是应该像assign属性的getter那样工作,其中getter不会保留并自动释放?(请参阅程序)
  1. I just feel that the getter of the weak property retains and autoreleases. Isn't it suppose to behave like getter of the assign property where the getter doesn't retain and autorelease ?(pls refer to the program)

程序:

我在程序下方给出了实际输出和预期输出.

I have given below the program with the actual output and my expected output.

注意-当我将属性从weak更改为assign时,满足了我的预期输出

Note - When I change property from weak to assign my expected output is met

#import<Foundation/Foundation.h>

@interface A : NSObject
- (void) dealloc;
@end

@implementation A
- (void) dealloc
{
    printf("\tinstance of A deallocated = %p\n", self);
}
@end

@interface B : NSObject
@property (weak) A* xa1;
- (void) dealloc;
@end

@implementation B
@synthesize xa1;
- (void) dealloc
{
    printf("\tinstance of B deallocated = %p\n", self);
}
@end


int main()
{
    B* b1 = [[B alloc] init];

    @autoreleasepool                        //autoreleasepool 1
    {   
        {                                   //block 1
            A* a1 = [[A alloc] init];
            printf("\ta1 = %p\n", a1);

            b1.xa1 = a1; 

            A* a3 = b1.xa1;

            printf("--- end of block 1\n");
        }                                       //at this point i expected instance pointed by a1 to be destroyed

        printf("--- end of autoreleasepool 1\n");
    }   

    printf("---- end of main\n");

    return(0);
}

实际输出:

    a1 = 0x10d713f50
--- end of block 1
--- end of autoreleasepool 1
    instance of A deallocated = 0x10d713f50
---- end of main
    instance of B deallocated = 0x10d713d30

我的预期输出:

    a1 = 0x10d713f50
--- end of block 1
    instance of A deallocated = 0x10d713f50
--- end of autoreleasepool 1
---- end of main
    instance of B deallocated = 0x10d713d30

谢谢

推荐答案

为属性提供weak会假定该ivar拥有__weak所有权,即,这只是@synthesize的一条指令.

Supplying weak on a property assumes __weak ownership for the ivar, i.e. it's just an instruction for @synthesize.

根据 http://clang.llvm.org/docs/AutomaticReferenceCounting.html§4.2,读取__weak变量需要保留对象(并在之后释放):

According to http://clang.llvm.org/docs/AutomaticReferenceCounting.html §4.2, reading __weak variable requires retaining the object (and releasing after, of course):

在对象左值上执行左值到右值转换时会发生读取.

Reading occurs when performing a lvalue-to-rvalue conversion on an object lvalue.

  • 对于__weak对象,将保留当前指针,然后在当前全表达式的末尾将其释放.这必须在任务分配和最终释放最终对象方面自动执行.
  • 对于所有其他对象,左值加载有原始语义.

它没有说明原因,但是想想如果从__weak变量获得的对象在您甚至开始使用它之前就死了会发生什么.弱指针的目的是确保您拥有nil或具有已知生命周期的有效对象,这就是为什么读取其值意味着保留pointee(然后属性的getter将其自动释放)的原因.

It does not say why, but think of what happens if the object you got from __weak variable dies before you even started using it. The purpose of weak pointer is to make sure you have either nil or a valid object with well-known lifetime, that is why reading its value implies retaining the pointee (and then property's getter returns it autoreleased).

这不是Obj-C独有的,它是所有弱指针实现(引用和垃圾回收)的惯用语.弱指针不能直接给出指针值,它们必须创建指向'hold'对象的强指针,以确保它不会在调用者甚至开始使用它之前就死掉.在Obj-C中,它是保留-自动释放;在C ++中,weak_ptr首先创建shared_ptr,在垃圾回收的环境中,将返回一个强引用,并且该对象的生存期将无提示地延长.

This is not unique to Obj-C, it's common idiom to all weak pointer implementations (both refcounted and garbage collected). Weak pointers can not give out pointer value directly, they have to create strong pointer to 'hold' object to make sure it won't die before caller has even started using it. In Obj-C, it's retain-autorelease; in C++, weak_ptr creates shared_ptr first, in garbage-collected environments, a strong reference is returned and the object's lifetime is prolonged silently.

这篇关于Objective-C-弱属性-吸气剂自动释放(自动引用计数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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