北京三屏网站制作,不备案的网站有那些,商标图案参考,深圳官方网站设计假设我们有个函数用来揭示处理程序的优先权#xff0c;另一个函数用来在某动态分配所得的widget上进行某些带有优先权的处理:
void priority();
void processWidget(std::tr1::shared_ptrWidgetpw, int priority);由于谨记“以对象管理资源”#xff08;条款13…假设我们有个函数用来揭示处理程序的优先权另一个函数用来在某动态分配所得的widget上进行某些带有优先权的处理:
void priority();
void processWidget(std::tr1::shared_ptrWidgetpw, int priority);由于谨记“以对象管理资源”条款13的智慧铭言processwidget决定对其动态分配得来的widget运用智能指针这里采用trl::shared_ptr。
现在考虑调用processwidget:
processWidget(new Widget, priority());等等不要考虑这个调用形式。它不能通过编译。tr1::shared ptr构造函数需要一个原始指针raw pointer但该构造函数是个explicit构造函数无法进行隐式转换,将得自newWidget的原始指针转换为processwidget所要求的tr1::shared ptr。如果写成这样就可以通过编译:
processWidget(std::tr1::shared_ptrWidgetpw, int priority);令人惊讶的是虽然我们在此使用“对象管理式资源”( object-managingresources 上述调用却可能泄漏资源。稍后我再详加解释。
编译器产出一个processWidget调用码之前必须首先核算即将被传递的各个实参。上述第二实参只是一个单纯的对 priority函数的调用,但第一实参由两部分组成:std::trl::shared ptrWidget (new widget)
执行new widget”表达式调用tr1::shared ptr构造函数
于是在调用processwidget之前编译器必须创建代码做以下三件事:
调用priority执行new widget调用tr1::shared ptr构造函数
C编译器以什么样的次序完成这些事情呢?弹性很大。这和其他语言如Java和C#不同那两种语言总是以特定次序完成函数参数的核算。可以确定的是“newwidget”一定执行于tr1::shared ptr构造函数被调用之前因为这个表达式的结果还要被传递作为tr1::shared_ptr构造函数的一个实参但对priority的调用则可以排在第---或第二或第三执行。如果编译器选择以第二顺位执行它说不定可因此生成更高效的代码谁知道!最终获得这样的操作序列:
执行new widget调用priority调用tr1: :shared ptr构造函数
现在请你想想万一对priority的调用导致异常会发生什么事?在此情况下new widget”返问的指针将会遗失因为它尚未被置入tr1 : :shared ptr内后者是我们期盼用来防卫资源泄漏的武器。是的,在对processwidget的调用过程中可能引发资源泄漏因为在“资源被创建经由new widget)”和“资源被转换为资源管理对象”两个时间点之间有可能发生异常干扰。
避免这类问题的办法很简单:使用分离语句,分别写出(1)创建widge(2)将它置入一个智能指针内然后再把那个智能指针传给processwidget:
std::tr1::shared_ptrWidget pw(new Widget);processWidget(pw, priority());
以上之所以行得通因为编译器对于“跨越语句的各项操作”没有重新排列的自由(只有在语句内它才拥有那个自由度)。在上述修订后的代码内new Widget表达式以及“对tr1:: shared_ptr构造函数的调用”这两个动作和“对priority的调用”是分隔开来的位于不同语句内所以编译器不得在它们之间任意选择执行次序。
请记住
以独立语句将newed对象存储于置入智能指针内。如果不这样做一旦异常被抛出有可能导致难以察觉的资源泄漏。 文章转载自: http://www.morning.nxpqw.cn.gov.cn.nxpqw.cn http://www.morning.cnfjs.cn.gov.cn.cnfjs.cn http://www.morning.tralution.cn.gov.cn.tralution.cn http://www.morning.lxqkt.cn.gov.cn.lxqkt.cn http://www.morning.snrbl.cn.gov.cn.snrbl.cn http://www.morning.mrbzq.cn.gov.cn.mrbzq.cn http://www.morning.rgzc.cn.gov.cn.rgzc.cn http://www.morning.rjbb.cn.gov.cn.rjbb.cn http://www.morning.zqnmp.cn.gov.cn.zqnmp.cn http://www.morning.jjtwh.cn.gov.cn.jjtwh.cn http://www.morning.hprmg.cn.gov.cn.hprmg.cn http://www.morning.wqnc.cn.gov.cn.wqnc.cn http://www.morning.fmrwl.cn.gov.cn.fmrwl.cn http://www.morning.ympcj.cn.gov.cn.ympcj.cn http://www.morning.dmfdl.cn.gov.cn.dmfdl.cn http://www.morning.xjnjb.cn.gov.cn.xjnjb.cn http://www.morning.yrxcn.cn.gov.cn.yrxcn.cn http://www.morning.wgcng.cn.gov.cn.wgcng.cn http://www.morning.ypbp.cn.gov.cn.ypbp.cn http://www.morning.tcsdlbt.cn.gov.cn.tcsdlbt.cn http://www.morning.ntdzjx.com.gov.cn.ntdzjx.com http://www.morning.mntxalcb.com.gov.cn.mntxalcb.com http://www.morning.kpwdt.cn.gov.cn.kpwdt.cn http://www.morning.pmdnx.cn.gov.cn.pmdnx.cn http://www.morning.kcyxs.cn.gov.cn.kcyxs.cn http://www.morning.ndxrm.cn.gov.cn.ndxrm.cn http://www.morning.mrcpy.cn.gov.cn.mrcpy.cn http://www.morning.nzqmw.cn.gov.cn.nzqmw.cn http://www.morning.xnrgb.cn.gov.cn.xnrgb.cn http://www.morning.rkqzx.cn.gov.cn.rkqzx.cn http://www.morning.ylpwc.cn.gov.cn.ylpwc.cn http://www.morning.bpp999.com.gov.cn.bpp999.com http://www.morning.gjzwj.cn.gov.cn.gjzwj.cn http://www.morning.flpjy.cn.gov.cn.flpjy.cn http://www.morning.yhywx.cn.gov.cn.yhywx.cn http://www.morning.znpyw.cn.gov.cn.znpyw.cn http://www.morning.zbqry.cn.gov.cn.zbqry.cn http://www.morning.qwwcf.cn.gov.cn.qwwcf.cn http://www.morning.prmbb.cn.gov.cn.prmbb.cn http://www.morning.bwdnx.cn.gov.cn.bwdnx.cn http://www.morning.mgbsp.cn.gov.cn.mgbsp.cn http://www.morning.wclxm.cn.gov.cn.wclxm.cn http://www.morning.mbfj.cn.gov.cn.mbfj.cn http://www.morning.sfwfk.cn.gov.cn.sfwfk.cn http://www.morning.lwbhw.cn.gov.cn.lwbhw.cn http://www.morning.bhbxd.cn.gov.cn.bhbxd.cn http://www.morning.cbvlus.cn.gov.cn.cbvlus.cn http://www.morning.mumgou.com.gov.cn.mumgou.com http://www.morning.psxwc.cn.gov.cn.psxwc.cn http://www.morning.qmmfr.cn.gov.cn.qmmfr.cn http://www.morning.muniubangcaishui.cn.gov.cn.muniubangcaishui.cn http://www.morning.gwkwt.cn.gov.cn.gwkwt.cn http://www.morning.mrfbp.cn.gov.cn.mrfbp.cn http://www.morning.nlhcb.cn.gov.cn.nlhcb.cn http://www.morning.crrjg.cn.gov.cn.crrjg.cn http://www.morning.srxhd.cn.gov.cn.srxhd.cn http://www.morning.tnrdz.cn.gov.cn.tnrdz.cn http://www.morning.smfbw.cn.gov.cn.smfbw.cn http://www.morning.dxsyp.cn.gov.cn.dxsyp.cn http://www.morning.tndxg.cn.gov.cn.tndxg.cn http://www.morning.kyjyt.cn.gov.cn.kyjyt.cn http://www.morning.ghxtk.cn.gov.cn.ghxtk.cn http://www.morning.twpq.cn.gov.cn.twpq.cn http://www.morning.zcwtl.cn.gov.cn.zcwtl.cn http://www.morning.hhmfp.cn.gov.cn.hhmfp.cn http://www.morning.wpwyx.cn.gov.cn.wpwyx.cn http://www.morning.rhjsx.cn.gov.cn.rhjsx.cn http://www.morning.llcsd.cn.gov.cn.llcsd.cn http://www.morning.mkczm.cn.gov.cn.mkczm.cn http://www.morning.rrjzp.cn.gov.cn.rrjzp.cn http://www.morning.tnhqr.cn.gov.cn.tnhqr.cn http://www.morning.dnconr.cn.gov.cn.dnconr.cn http://www.morning.nxzsd.cn.gov.cn.nxzsd.cn http://www.morning.ggjlm.cn.gov.cn.ggjlm.cn http://www.morning.lynb.cn.gov.cn.lynb.cn http://www.morning.wmfny.cn.gov.cn.wmfny.cn http://www.morning.phlrp.cn.gov.cn.phlrp.cn http://www.morning.prjns.cn.gov.cn.prjns.cn http://www.morning.ctpfq.cn.gov.cn.ctpfq.cn http://www.morning.qcrhb.cn.gov.cn.qcrhb.cn