NSNotificationCenter postNotificationName crash

postNotificationName:XXXXXX carsh

[[NSNotificationCenter defaultCenter] postNotificationName:XXXXXX object:self];

經(jīng)過(guò)推理得出結(jié)論:
因?yàn)橥ㄖ侨之惒降?self有可能被釋放掉了,或許通知沒用對(duì)數(shù)據(jù)加鎖or something
網(wǎng)上搜索也得出了相似的答案

  • I've seen no documentation that promises that. You can try to explore it by making dealloc very slow (using sleep() for instance) and adding printf() logging. (Never do threading analysis with NSLog.) But I doubt you could rely on the results. Even if ARC promised it, you can't know whether NSNotificationCenter uses ARC internally. The only real promises are what you get from the docs. – Rob Napier Dec 6 '12 at 18:05

這樣使用為什么會(huì)有崩潰問(wèn)題呢?
因?yàn)闀?huì)隨機(jī)遇見僵尸內(nèi)存問(wèn)題

Are there any workaround for randomly crash?

Enable zombies - this will cause the exception to breakpoint on the offending line. From there it will be a lot easier to figure out than the general EXC_BAD_ACCESS crash.

那如何解決呢?
1 .翻看sdwebimage源碼看到了如下代碼(本人未驗(yàn)證)(以測(cè)試,沒有用)

__block typeof(self) strongSelf = self;

[[NSNotificationCenter defaultCenter] postNotificationName:XXXXXX object:strongSelf ];

2.轉(zhuǎn)地址(本人未驗(yàn)證)
地址獲取與此類似

__block typeof(self) strongSelf = self;
NSInteger  nSelf = (long)(__bridge void *)self;
NSNumber  *number = @(nSelf);
[[NSNotificationCenter defaultCenter] postNotificationName:XXXXXX object:number];

//接受通知對(duì)象轉(zhuǎn)換成self
NSObject *obj = (__bridge NSObject*) object.integerValue;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容