Objective-C-弱对象自动在autoreleasepool中注册? [英] Objective-C - weak object is registered in autoreleasepool automatically?

查看:131
本文介绍了Objective-C-弱对象自动在autoreleasepool中注册?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读由坂本一树(Kazuki Sakamoto)编写的带有ARC,Grand Central Dispatch和Blocks的iOS和OS X Pro多线程和内存管理专业版.

I am reading Pro Multithreading and Memory Management for iOS and OS X with ARC, Grand Central Dispatch, and Blocks written by Kazuki Sakamoto.

这本书写道:

使用带有__weak限定词的变量时,该对象始终为 在autoreleasepool中注册.

When a variable with a __weak qualifier is used, the object is always registered in autoreleasepool.

id __weak obj1 = obj0;
NSLog(@"class=%@", [obj1 class]);

上面的源代码等效于:

id __weak obj1 = obj0;
id __autoreleasing tmp = obj1;
NSLog(@"class=%@", [tmp class]);

为什么需要按顺序在autoreleasepool中注册对象 通过__weak限定变量使用对象?因为一个 合格为__weak的变量不具有强 参考,该对象可能在任何时候被丢弃.如果对象 已在autoreleasepool中注册,直到@autoreleasepool块被 左,该对象必须存在.因此,要通过__weak使用对象 安全地变量,该对象已在autoreleasepool中注册 自动.

Why does the object need to be registered in autoreleasepool in order to use the object via the __weak qualified variable? Because a variable, which is qualified with __weak, does not have a strong reference, the object might be disposed of at any point. If the object is registered in autoreleasepool, until @autoreleasepool block is left, the object must exist. So, to use the objects via __weak variable safely, the object is registered in autoreleasepool automatically.

我在程序下方给出了验证作者写的内容的信息.

I have given below the program to verify what the author wrote.

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

@implementation A
- (void) dealloc
{
    NSLog(@"dealloc");
}
@end


int main()
{
    @autoreleasepool
    {
        {
            A *obj0 = [[A alloc] init];
            id __weak obj1 = obj0;
            NSLog(@"end of block");
        }
        NSLog(@"end of autoreleasepool");
    }

    return(0);
}

实际输出:

end of block
dealloc
end of autoreleasepool

预期输出:

end of block
end of autoreleasepool
dealloc

obj1位于该块的末尾.显然obj1不会自动注册到autoreleasepool中.

obj1 is disposed at the end of the block. It's obvious that obj1 isn't registered in autoreleasepool automatically.

如果我手动添加

id __autoreleasing tmp = obj1;

预期输出. obj1位于自动释放池的末尾.

The output is expected. obj1 is disposed at the end of the autoreleasepool.

谁能告诉我这是怎么回事?弱对象是否会自动注册到autoreleasepool中?

Could anyone tell me what's going on? Is the weak object registered in autoreleasepool automatically?

谢谢!

推荐答案

使用带有__weak限定词的变量时,该对象始终为 在autoreleasepool中注册.

When a variable with a __weak qualifier is used, the object is always registered in autoreleasepool.

那是绝对的,完全是胡说八道. __weak与自动释放池无关.

That is absolute and total nonsense. __weak has nothing to do with autorelease pools whatsoever.

编译器所做的是将指针的位置添加到弱指针"位置列表中-注意指针而不是对象的位置.因此,当对象本身被释放时,指向它的所有弱变量都可以设置为nil.

What the compiler does is add the location of the pointer to the list of "weak pointer" locations - note the location of the pointer, not the object. So when the object itself is released, all weak variables pointing to it can be set to nil.

当然,编译器很聪明,并且在可以证明不必要的情况下,它将对其进行优化.

And of course the compiler is clever and will optimise that kind of thing away when it can prove that it is not necessary.

作者所说的安全使用弱变量是胡说八道.弱变量的整个 point 是它不能使对象保持活动状态.因此,防止物体离开绝对是胡说八道.整个想法是它可以消失,因此有时您将检查对象是否仍然存在.

What the author says about using weak variables safely is nonsense. The whole point of a weak variable is that it doesn't keep the object alive. So preventing the object from going away would be absolute nonsense. The whole idea is that it can go away, so sometimes you will check if the object is still there.

我没有这本书,所以我不知道您是否正确地引用了这本书.如果引用正确,则说明该书是在胡说八道,应该避免.好吧,老实说,书名中的"Pro Multithreading"已经引起争议.

I don't have the book, so I cannot know if you are quoting the book correctly. If you are quoting it correctly, then the book is spouting dangerous nonsense and should be avoided. Well, to be honest, "Pro Multithreading" in a book title is off-putting already.

谁能告诉我这是怎么回事?弱对象是否已注册? autoreleasepool自动吗?

Could anyone tell me what's going on? Is the weak object registered in autoreleasepool automatically?

当然,绝对不是.那绝对是愚蠢的.

Absolutely, definitely not. That would be absolutely stupid.

这篇关于Objective-C-弱对象自动在autoreleasepool中注册?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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