有关" Use After Free"的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
11232as
V2EX    C++

有关" Use After Free"的问题

  •  
  •   11232as Apr 30, 2021 2900 views
    This topic created in 1826 days ago, the information mentioned may be changed or developed.

    C++学艺不精看不懂这个错,请各位大佬帮忙看一下这里为什么会报一个Use After Free啊。

    Result<pcrepp, pcrepp::compile_error> pcrepp::from_str(std::string pattern, int options) { const char *errptr; int eoff; auto code = pcre_compile(pattern.c_str(), options | PCRE_UTF8, &errptr, &eoff, nullptr); if (!code) { return Err(compile_error{errptr, eoff}); } return Ok(pcrepp(std::move(pattern), code));//这一行报错 } //报错信息 //call to `Result` eventually accesses memory that was invalidated by call to `free()` on line 86 indirectly during the call to `Ok`. //84. } //85. //86. return Ok(pcrepp(std::move(pattern), code)); // ^ //87. } //88. 

    Ok方法

    template<typename T, typename CleanT = typename std::decay<T>::type> types::Ok<CleanT> Ok(T&& val) { return types::Ok<CleanT>(std::forward<T>(val)); } 

    Result的构造函数

     Result(types::Ok<T> ok) : ok_(true) { storage_.construct(std::move(ok)); } 

    谢谢各位大佬了[抱拳]

    Supplement 1    Apr 30, 2021

    storage_.construct 的部分

     void construct(types::Ok<T> ok) { new (&storage_) T(std::move(ok.val)); initialized_ = true; } 
    5 replies    2021-05-06 17:24:39 +08:00
    Inn0Vat10n
        1
    Inn0Vat10n  
       Apr 30, 2021
    Ok 的这一层转换,拿 左值引用 引用 右值了
    yazoox
        2
    yazoox  
       Apr 30, 2021
    楼主,请教一下。
    template<typename T, typename CleanT = typename std::decay<T>::typ>
    你这里,定义模板,最后这个 std::decay<T>::type 为什么要添加?我看你下面函数体,也没有用到呢......
    wutiantong
        3
    wutiantong  
       Apr 30, 2021
    可能错在 storage_.construct 这里面
    araraloren
        4
    araraloren  
       Apr 30, 2021
    我还以为我在看 rust 。。
    wutiantong
        5
    wutiantong  
       May 6, 2021
    https://github.com/tstack/lnav

    这么高关注度的项目,为啥不去提 issue 呢?
    About     Help     Advertise     Blog     API     FAQ     Solana     2639 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 46ms UTC 11:57 PVG 19:57 LAX 04:57 JFK 07:57
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86