简体   繁体   English

块中的@synchronized(self)是否会导致保留周期?

[英]Does @synchronized(self) in a block lead to a retain cycle?

Let's say I want to do @synchronized(self) within a block. 假设我想在一个块内执行@synchronized(self) I suppose this will lead to a retain cycle, so normally we would re-write it like this: 我想这将导致一个保留周期,因此通常我们将这样重写它:

-(void)myMethod
{
    __weak TheClass * weakSelf = self;
    dispatch_async(dispatch_get_main_queue(),
    ^{
        TheClass * strongSelf = weakSelf;
        if(strongSelf == nil)
        {
            return;
        }

        @synchronized(strongSelf)
        {
            //mutex code
        }
    }
}

My question is, when you use the @synchronized directive this way, is it equivalent to @synchronized(self) ? 我的问题是,当您以这种方式使用@synchronized指令时,它是否等同于@synchronized(self)

Short answer: No 简短答案:否

Longer answer: 更长的答案:

Background 背景

For there to be a cycle involving a block the block must reference another object, and that object must (directly or via a longer chain) reference the block. 为了有一个涉及一个块的循环 ,该块必须引用另一个对象,并且该对象必须(直接或通过更长的链)引用该块。

A cycle in of itself is not bad, it is only bad if it results in the lifetime of objects in the cycle being extended past the point those objects are required. 循环本身并不坏,仅当它导致循环中的对象寿命延长到需要这些对象的时间点时,这才是不好的。 It is fine to create a cycle as long as cycle is broken - by breaking one of the links forming the cycle - at some point. 只要在某个时候断开循环(通过断开形成循环的链接之一),就可以创建一个循环。

A construct like: 像这样的构造:

__weak TheClass * weakSelf = self;
...
self.blockVar = ^{
    TheClass * strongSelf = weakSelf;
    ...

prevents a static cycle being created as (the object referenced by) self strongly references (the object referenced by - you get the idea, the variable isn't important but the thing being referenced by it) blockVar but blockVar only has a weak reference back to self . 防止由于以下原因而创建静态循环: self引用(被引用的对象-被您引用的对象-您知道,变量并不重要,但被其引用的对象) blockVarblockVar仅具有弱引用self

However every time the block is executed it creates a strong reference (stored in strongSelf ) to self and so creates a dynamic cycle - which is automatically broken when the block finishes executing. 但是,每次执行该块时,它都会创建一个对self的强引用(存储在strongSelf ),因此会创建一个动态循环-当该块完成执行时,该循环会自动中断。

Your code 您的密码

  1. Look at your code, you create a block and pass it directly to dispatch_async - you never store a reference to the block in self . 查看您的代码,创建一个块并将其直接传递给dispatch_async您永远不会在self存储对该块的引用。 So there never is any cycle here, no need to mess with weak references at all. 因此,这里从来没有任何循环,根本不需要弄乱弱引用。

  2. Once the block creates strongSelf there is a cycle, then using @synchronized(strongSelf) doesn't create a second one, it just takes a lock on the object. 一旦该块创建了strongSelf就存在一个循环,然后使用@synchronized(strongSelf)不会创建第二个循环,它只是对对象进行了锁定。 When the synchronized statement exits the lock goes, when the block exits the strong cycle goes. 当同步语句退出时,锁消失,当块退出时,强循环消失。

HTH 高温超导

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在这个区块中强烈捕获自我可能会导致保留周期 - capturing self strongly in this block is likely to lead to a retain cycle 避免“在此块中强烈捕获自我可能会导致保留周期”消息 - Avoiding the “capturing self strongly in this block is likely to lead to a retain cycle” message “使用可达性”“在此区块中强烈捕获'自我'很可能导致保留周期” - “Capturing 'self' strongly in this block is likely to lead to a retain cycle” using Reachability iOS 5 Twitter Framework和completionHandler块 - “在这个块中强烈捕获'自我'可能会导致保留周期” - iOS 5 Twitter Framework & completionHandler block - “Capturing 'self' strongly in this block is likely to lead to a retain cycle” 在这种情况下,如何解决警告“在此块中强烈捕获‘自我’可能会导致保留周期”? - How to resolve warning "Capturing 'self' strongly in this block is likely to lead to a retain cycle" in this case? 如何在此块中强烈修复“捕获'块'可能会导致保留周期” - How to fix “Capturing 'block' strongly in this block is likely to lead to a retain cycle” iOS区块释放和自我保留周期 - iOS block release and self retain cycle 如果在块内引用self,为什么这不是保留周期? - Why is this not a retain cycle if self is referred to within a block? 什么时候在块中引用自身? - When is reference to self in block a retain cycle? 阻塞对象会导致保留周期吗? - Does block object cause retain cycle?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM