在Objective C中,什么时候ARC下发布了弱对象?

时间:2021-02-02 09:37:24

Since the reference count becoming 0 will cause the object being released. I am curious when is a weak object released since the count is always 0, when I declare a weak one, do I need to worry about it will be released half way?

由于引用计数变为0将导致对象被释放。我好奇当一个弱对象被释放,因为计数总是0,当我宣布一个弱的时候,我是否需要担心它会被释放一半?

For example

NSObject ClassA

@property (weak) NSString stringA;


- init() {
    ...
    stringA = @"this is a weak string";
    ...
}

- doSomething() {
    // When I call this function later, 
    // is there any chance at this point stringA has been released?
    NSLog(stringA);
}

2 个解决方案

#1


You would only declare a weak property if it was connected via an IBOutlet or a as a delegate/datasource (references another UIViewController).

如果它通过IBOutlet或作为委托/数据源连接(引用另一个UIViewController),则只声明一个弱属性。

If you make a weak property, it will be released immediately after instantiating it. However, a weak property connected via an IBOutlet will not release because the view holds strongly to the property.

如果你创建一个弱属性,它将在实例化后立即释放。但是,通过IBOutlet连接的弱属性将不会释放,因为视图对该属性具有强烈支持。

Same with properties of type VCs, such as delegates, they are weak properties because you assign your class to the property. The VC is obviously held strongly so the delegate should refrain from holding strongly to the VC to prevent retain cycles (where a holds strongly to b and b holds strongly to a).

与VC类型的属性相同,例如委托,它们是弱属性,因为您将类分配给属性。 VC显然是强有力的,因此代表应该避免强行保持VC以防止保留周期(其中a强烈保持b和b强烈保持a)。

So to answer your question, a weak property will be released immediately if nothing holds strongly to it, and the above is scenarios where you will use a weak property.

因此,为了回答你的问题,如果没有任何强烈的属性,将立即释放弱属性,以上是你将使用弱属性的情况。

#2


Strings are a bad example. There are certain objects that get never released. Constant strings, [NSNull null], @YES and @NO, small NSNumber values in 32 bit and many NSNumber values in 64 bit, empty arrays, and so on. Since they are not released, a weak variable will never be nil.

字符串是一个坏例子。有些物品永远不会被释放。常量字符串,[NSNull null],@ YES和@NO,32位的小NSNumber值和64位空数组中的许多NSNumber值,依此类推。由于它们没有被释放,弱变量永远不会是零。

Many objects are autoreleased. If nothing else references them but their autorelease pool, they go away when the pool goes away.

许多对象都是自动释放的。如果没有别的东西引用它们而不是它们的自动释放池,它们会在游泳池消失时消失。

But if you create an object with alloc/init and store it in a weak variable, the compiler knows that alloc/init had a reference count of 1, that reference count is removed, and poof! it goes. If you store the reference into a local variable first which is strong by default, it goes away when the code leaves the scope of the local variable. If you store into a weak variable first and then from the weak variable immediately into a strong one, it is too late, it's gone.

但是如果使用alloc / init创建一个对象并将其存储在弱变量中,编译器就会知道alloc / init的引用计数为1,该引用计数被删除,并且poof!它去了。如果将引用存储在一个默认为强的局部变量中,则当代码离开局部变量的范围时它会消失。如果你首先存储到弱变量然后从弱变量存储到强变量中,那就太晚了,它已经消失了。

#1


You would only declare a weak property if it was connected via an IBOutlet or a as a delegate/datasource (references another UIViewController).

如果它通过IBOutlet或作为委托/数据源连接(引用另一个UIViewController),则只声明一个弱属性。

If you make a weak property, it will be released immediately after instantiating it. However, a weak property connected via an IBOutlet will not release because the view holds strongly to the property.

如果你创建一个弱属性,它将在实例化后立即释放。但是,通过IBOutlet连接的弱属性将不会释放,因为视图对该属性具有强烈支持。

Same with properties of type VCs, such as delegates, they are weak properties because you assign your class to the property. The VC is obviously held strongly so the delegate should refrain from holding strongly to the VC to prevent retain cycles (where a holds strongly to b and b holds strongly to a).

与VC类型的属性相同,例如委托,它们是弱属性,因为您将类分配给属性。 VC显然是强有力的,因此代表应该避免强行保持VC以防止保留周期(其中a强烈保持b和b强烈保持a)。

So to answer your question, a weak property will be released immediately if nothing holds strongly to it, and the above is scenarios where you will use a weak property.

因此,为了回答你的问题,如果没有任何强烈的属性,将立即释放弱属性,以上是你将使用弱属性的情况。

#2


Strings are a bad example. There are certain objects that get never released. Constant strings, [NSNull null], @YES and @NO, small NSNumber values in 32 bit and many NSNumber values in 64 bit, empty arrays, and so on. Since they are not released, a weak variable will never be nil.

字符串是一个坏例子。有些物品永远不会被释放。常量字符串,[NSNull null],@ YES和@NO,32位的小NSNumber值和64位空数组中的许多NSNumber值,依此类推。由于它们没有被释放,弱变量永远不会是零。

Many objects are autoreleased. If nothing else references them but their autorelease pool, they go away when the pool goes away.

许多对象都是自动释放的。如果没有别的东西引用它们而不是它们的自动释放池,它们会在游泳池消失时消失。

But if you create an object with alloc/init and store it in a weak variable, the compiler knows that alloc/init had a reference count of 1, that reference count is removed, and poof! it goes. If you store the reference into a local variable first which is strong by default, it goes away when the code leaves the scope of the local variable. If you store into a weak variable first and then from the weak variable immediately into a strong one, it is too late, it's gone.

但是如果使用alloc / init创建一个对象并将其存储在弱变量中,编译器就会知道alloc / init的引用计数为1,该引用计数被删除,并且poof!它去了。如果将引用存储在一个默认为强的局部变量中,则当代码离开局部变量的范围时它会消失。如果你首先存储到弱变量然后从弱变量存储到强变量中,那就太晚了,它已经消失了。