码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • C++ debug 系列


    struct A
    {
        vector<int> v;
        int a;
    
        A(int _a=0)
        {
            cout<<"construct!"<<endl;
            a=_a;
            new (&v) vector<int>(_a,0);
        }
    
        
        ~A()
        {
            cout<<"destroy!"<<endl;
            v.~vector<int>();
            a=0;
        }
    } fxxk;
    
    
    void main()
    {
        for (int i=0;i<100;i++)
        {
            fxxk.~A();
            cout<<1<<endl;
            new (&fxxk) A(100000);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    来自学弟的一个程序,在进行了如下输出之后会 RE。
    在这里插入图片描述

    原因

    对象析构时在自己的析构函数执行完毕后会依次调用成员的析构函数。

    ~A() 里面的 ~vector() 多此一举,导致了 double free

    其他

    placement new 的正规操作:

    1. 首先持有可用的内存空间,常用途径有申请字符数组,内存池等。
    2. placement new 分配对象 T * t = new (ptr) T
    3. 正常使用对象 t.exec()
    4. 销毁对象 t.~T()
    5. 重复循环 2-4,直到结束
    6. 归还内存空间

    代码中 main 函数对 placement new 的使用是没问题的。

    update

    有人问既然析构会导致 double free,为什么第一次析构没有出现?

    这是因为第一次构造的 vector 是平凡的,它的三个指针(start,end,end_cap)都是 nullptr ,不需要析构。

    template <class _Tp, class _Allocator>
    __vector_base<_Tp, _Allocator>::~__vector_base()
    {
        if (__begin_ != nullptr)
        {
            clear();
            __alloc_traits::deallocate(__alloc(), __begin_, capacity());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    所以更简单的复现 case 只需要构造一个非平凡的 A,不用进行任何的操作就可以复现 crash

    #include 
    #include 
    
    using namespace std;
    
    struct A
    {
        vector<int> v;
        int a;
    
        A(int _a=0)
        {
            cout<<"construct!"<<endl;
            a=_a;
            new (&v) vector<int>(_a,0);
        }
    
        ~A()
        {
            cout<<"destroy!"<<endl;
            v.~vector<int>();
            a=0;
        }
    } fxxk(1);
    
    int main() {
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    /Users/lf/repos/untitled/cmake-build-debug/untitled
    construct!
    destroy!
    untitled(25973,0x11daf5e00) malloc: *** error for object 0x7feb1d6043f0: pointer being freed was not allocated
    untitled(25973,0x11daf5e00) malloc: *** set a breakpoint in malloc_error_break to debug
    
    Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 相关阅读:
    经典排序——帮你解决关于排序的绝大多数问题
    在Qt创建的UI中放一个显示点云的窗口(PCL+QT5)
    Java学习笔记(三)
    CompletableFuture异步编程详解
    Spring更简单的读取和存储
    【Redis学习笔记】第十一章 Redis主从复制
    通过Xshell7连接云服务Linux系统级上传文件
    Deep Residual Learning for Image Recognition浅读与实现
    高層建築設計和建造:從避難層到設備間和防風防火防水的設計理念,酒店住宅辦公樓都有什麽房間(精簡)
    一文看懂 Camera2 framework:以具体 preview 流程为例 独家原创
  • 原文地址:https://blog.csdn.net/m0_37809890/article/details/127557556
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号