I have the following code, expecting that once obj2 is released obj1 is still retained, will it work?
我有以下代码,期望一旦obj2被释放,obj1仍然保留,它会工作吗?
obj1 = [[Class1 alloc] init];
obj2 = [[obj1 retain] autorelease];
2 个解决方案
#1
4
I have the following code, expecting that once obj2 is released obj1 is still retained, will it work?
我有以下代码,期望一旦obj2被释放,obj1仍然保留,它会工作吗?
obj1
and obj2
are both pointers to the same object. That object gets a retain count of 1 when you create it. The object's retain count increases to 2 when you retain it again on the second line. The object (and any pointers to it) will therefore remain valid until it's released twice. One of those releases will eventually come from the autorelease
on the second line; another should be supplied by your code somewhere.
obj1和obj2都是指向同一对象的指针。创建它时,该对象的保留计数为1。当您在第二行再次保留时,对象的保留计数增加到2。因此,对象(以及它的任何指针)将保持有效,直到它被释放两次。其中一个版本最终将来自第二行的自动发布;另一个应该由你的代码在某处提供。
#2
2
It'll work, but I'm not sure your conclusion is correct (had a parse error on that).
它会起作用,但我不确定你的结论是否正确(有一个解析错误)。
obj1 = [[Class1 alloc] init];
obj2 = [[obj1 retain] autorelease];
It helps to think of retain and release in terms of delta and per-reference. So, in the following code, you would say that "the obj1 reference has a retain count of +1" and "the obj2 references has a retain count of +0".
根据增量和每个引用来考虑保留和释放是有帮助的。因此,在下面的代码中,您会说“obj1引用的保留计数为+1”,“obj2引用的保留计数为+0”。
That is, obj1
is a strong reference and obj2
is a weak reference.
也就是说,obj1是强引用,obj2是弱引用。
If you want the object to go away, you would [obj1 release]
. You could [obj2 release]
, but that implies an ownership transfer which should be made only for good reason and quite explicitly.
如果你想让对象消失,你会[obj1发布]。您可以[obj2发布],但这意味着所有权转移应该只是出于充分的理由而且非常明确地进行。
Or you could just turn on ARC and be done with it.
或者你可以打开ARC并完成它。
#1
4
I have the following code, expecting that once obj2 is released obj1 is still retained, will it work?
我有以下代码,期望一旦obj2被释放,obj1仍然保留,它会工作吗?
obj1
and obj2
are both pointers to the same object. That object gets a retain count of 1 when you create it. The object's retain count increases to 2 when you retain it again on the second line. The object (and any pointers to it) will therefore remain valid until it's released twice. One of those releases will eventually come from the autorelease
on the second line; another should be supplied by your code somewhere.
obj1和obj2都是指向同一对象的指针。创建它时,该对象的保留计数为1。当您在第二行再次保留时,对象的保留计数增加到2。因此,对象(以及它的任何指针)将保持有效,直到它被释放两次。其中一个版本最终将来自第二行的自动发布;另一个应该由你的代码在某处提供。
#2
2
It'll work, but I'm not sure your conclusion is correct (had a parse error on that).
它会起作用,但我不确定你的结论是否正确(有一个解析错误)。
obj1 = [[Class1 alloc] init];
obj2 = [[obj1 retain] autorelease];
It helps to think of retain and release in terms of delta and per-reference. So, in the following code, you would say that "the obj1 reference has a retain count of +1" and "the obj2 references has a retain count of +0".
根据增量和每个引用来考虑保留和释放是有帮助的。因此,在下面的代码中,您会说“obj1引用的保留计数为+1”,“obj2引用的保留计数为+0”。
That is, obj1
is a strong reference and obj2
is a weak reference.
也就是说,obj1是强引用,obj2是弱引用。
If you want the object to go away, you would [obj1 release]
. You could [obj2 release]
, but that implies an ownership transfer which should be made only for good reason and quite explicitly.
如果你想让对象消失,你会[obj1发布]。您可以[obj2发布],但这意味着所有权转移应该只是出于充分的理由而且非常明确地进行。
Or you could just turn on ARC and be done with it.
或者你可以打开ARC并完成它。