I know the instance variable in ARC are by default __strong
. How can I release an instance variable when the containing class is still retained. In the following example v
is __strong
我知道ARC中的实例变量默认是__strong。如何在仍保留包含类时释放实例变量。在以下示例中,v是__strong
and c
is allocated when object of A is created some where and retained. I want to release the c
instance variable. How to should I do that?, What should be in releaseC
method that will release the c
instance variable.
当A的对象在某处创建并保留时,分配c和c。我想发布c实例变量。我应该怎么做?,releaseC方法应该释放c实例变量。
@interface A {
Obj *c;
}
@implementation A {
- (id)init {
if((self = [super init])){
c = [[Obj alloc] init];
}
return self;
}
- (void)releaseC {
//what should be here?
}
}
5 个解决方案
#1
2
Obj *c; = [[Obj alloc] init];
- (void)releaseC {
c = nil;
}
You cannot directly control when an object is released BUT you can indirectly cause it to happen. How? Remember what ARC does EXACTLY. Unlike human coding convention, ARC parses your code and inserts release statements AS SOON AS OBJECTS CAN be released. This frees up the memory for new allocations straight away, which is awesome/necessary. Meaning, setting an object to nil, or simply allowing a variable to go out of scope ... something that CAUSES A 0 RETAIN COUNT forces ARC to place its release calls there. It must ... because it would leak otherwise.
您不能直接控制何时释放对象但您可以间接导致它发生。怎么样?记住ARC确实做了什么。与人类编码约定不同,ARC解析您的代码并插入发布语句,因为即使是对象也可以发布。这可以立即释放内存以进行新的分配,这非常棒/必要。意思是,将对象设置为nil,或者只是允许变量超出范围......导致A 0 RETAIN COUNT的事情迫使ARC在其中放置其释放调用。它必须......因为否则会泄漏。
#2
2
- (void)releaseC {
c = nil;
}
#3
2
c = nil;
c =零;
But some would argue it isn't productive from an efficiency standpoint. And while the release will be immediate in the sense it isn't any longer usable, the memory may not be freed immediately.
但有些人认为,从效率的角度来看,效率并不高。虽然从某种意义上讲它将立即释放,但它不再可用,但内存可能无法立即释放。
#4
0
there is no need to release the variable in ARC. it done automatically
无需在ARC中释放变量。它自动完成
#5
0
You are probably miss understanding what you want to do. I suppose you want to release the variable for memory issues. All you have to do is nil it. Instance variables are pointers to objects. As long as an object is pointed by something it is kept alive. As soon as you dont need something you can "stop pointing at it" and it will be released automagically.
您可能想念自己想要做什么。我想你想要释放内存问题的变量。你所要做的就是零。实例变量是指向对象的指针。只要一个物体被某物指向就会保持活着。一旦你不需要什么,你就可以“停止指向它”,它将自动发布。
As for the design, I am not so sure why you would have a public method that releases an instance variable. (I'm assuming its public because if it was not you would just nil it without actually having to write a method). If you do indeed intend to be able to release an instance variable from outside the class, I would simply make the Instance variable public and release it from anywhere setting it as nil.
至于设计,我不太清楚为什么你会有一个释放实例变量的公共方法。 (我假设它是公开的,因为如果它不是你只需要它而不必实际编写方法)。如果你确实打算能够从类外部释放一个实例变量,我只需将Instance变量公开并从任何地方释放它,将其设置为nil。
#1
2
Obj *c; = [[Obj alloc] init];
- (void)releaseC {
c = nil;
}
You cannot directly control when an object is released BUT you can indirectly cause it to happen. How? Remember what ARC does EXACTLY. Unlike human coding convention, ARC parses your code and inserts release statements AS SOON AS OBJECTS CAN be released. This frees up the memory for new allocations straight away, which is awesome/necessary. Meaning, setting an object to nil, or simply allowing a variable to go out of scope ... something that CAUSES A 0 RETAIN COUNT forces ARC to place its release calls there. It must ... because it would leak otherwise.
您不能直接控制何时释放对象但您可以间接导致它发生。怎么样?记住ARC确实做了什么。与人类编码约定不同,ARC解析您的代码并插入发布语句,因为即使是对象也可以发布。这可以立即释放内存以进行新的分配,这非常棒/必要。意思是,将对象设置为nil,或者只是允许变量超出范围......导致A 0 RETAIN COUNT的事情迫使ARC在其中放置其释放调用。它必须......因为否则会泄漏。
#2
2
- (void)releaseC {
c = nil;
}
#3
2
c = nil;
c =零;
But some would argue it isn't productive from an efficiency standpoint. And while the release will be immediate in the sense it isn't any longer usable, the memory may not be freed immediately.
但有些人认为,从效率的角度来看,效率并不高。虽然从某种意义上讲它将立即释放,但它不再可用,但内存可能无法立即释放。
#4
0
there is no need to release the variable in ARC. it done automatically
无需在ARC中释放变量。它自动完成
#5
0
You are probably miss understanding what you want to do. I suppose you want to release the variable for memory issues. All you have to do is nil it. Instance variables are pointers to objects. As long as an object is pointed by something it is kept alive. As soon as you dont need something you can "stop pointing at it" and it will be released automagically.
您可能想念自己想要做什么。我想你想要释放内存问题的变量。你所要做的就是零。实例变量是指向对象的指针。只要一个物体被某物指向就会保持活着。一旦你不需要什么,你就可以“停止指向它”,它将自动发布。
As for the design, I am not so sure why you would have a public method that releases an instance variable. (I'm assuming its public because if it was not you would just nil it without actually having to write a method). If you do indeed intend to be able to release an instance variable from outside the class, I would simply make the Instance variable public and release it from anywhere setting it as nil.
至于设计,我不太清楚为什么你会有一个释放实例变量的公共方法。 (我假设它是公开的,因为如果它不是你只需要它而不必实际编写方法)。如果你确实打算能够从类外部释放一个实例变量,我只需将Instance变量公开并从任何地方释放它,将其设置为nil。