使用boost::enable_shared_from_this的注意事項(xiàng)
掃描二維碼
隨時(shí)隨地手機(jī)看文章
[轉(zhuǎn)自]lemonClover的日記
使用boost::enable_shared_from_this的注意事項(xiàng)2011-10-26 16:44:26?1. 如果一個(gè)類(lèi)繼承了boost::enable_shared_from_this,那么之后創(chuàng)建這個(gè)類(lèi)的對(duì)象的時(shí)候,一定要用shared_ptr封裝起來(lái)。
?2. 一定不要在構(gòu)造函數(shù)和析構(gòu)函數(shù)里面調(diào)用shared_from_this(),否則會(huì)引起十分詭異的錯(cuò)誤。
?昨天就是因?yàn)樵谖鰳?gòu)函數(shù)里面調(diào)用了另一個(gè)函數(shù),而這個(gè)函數(shù)里面使用了shared_from_this(),導(dǎo)致運(yùn)行時(shí)出現(xiàn)bad_weak_ptr的錯(cuò)誤,調(diào)試了一個(gè)晚上和一個(gè)上午才發(fā)現(xiàn)。
【原因分析】
為什么不能在構(gòu)造函數(shù)中調(diào)用shared_from_this