capturing self strongly in this block is likely to lead to a retain cycle,capturingretain

來源:互聯網
上載者:User

capturing self strongly in this block is likely to lead to a retain cycle,capturingretain

一個使用Block文法的執行個體變數,在引用另一個執行個體變數的時候,經常會引起retain cycle。

capturing self strongly in this block is likely to lead to a retain cycle


_items = [[NSMutableArray alloc] init];      _block = ^{          [_items addObject:@"Hello!"]; //_block引用了_items,導致retain cycle。      }; 

寫成下面格式

__block ViewController *blockSelf = self;  _block = ^{      [blockSelf->_items addObject:@"Hello!"];  };



Capturing self strongly in this block is likely to lead to retain cycle

block 是代碼塊的意思。
甲裡引用乙,乙裡引用甲,就會導致 “retain cycle” -- “形成怪圈”的錯誤。
網上資料:
解決的辦法是 建立一個本地變數 指向 自己(self).
例如:_items 實際上是 self->items,
_block 對象在建立的時候會被retain一次
改成:
__block ViewController *blockSelf = self;
_block = ^{
[blockSelf->_items addObject:@"Hello!"];
};
就可以了。
-------------------

DoSomethingManager *manager = [[DoSomethingManager alloc] init];

manager.complete = ^{

//...complete actions

[manager otherAction];

manager.complete = nil; // Objective-C 用這種方法破壞怪圈。

[manager release];

};
-------------
 
報一個異常,問怎可以消除

解析出了問題,是木馬程式
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.