• 链表错误:AddressSanitizer: heap-use-after-free on address


    在写链表时会遇见的错误

    ERROR: AddressSanitizer: heap-use-after-free on address 0x603000000258 at pc 0x00000037c6f5 bp 0x7ffea5071f90 sp 0x7ffea5071f88
    READ of size 8 at 0x603000000258 thread T0
    #3 0x7f3fea5be082 (/lib/x86_64-linux-gnu/libc.so.6+0x24082)
    0x603000000258 is located 8 bytes inside of 24-byte region [0x603000000250,0x603000000268)
    freed by thread T0 here:
    #3 0x7f3fea5be082 (/lib/x86_64-linux-gnu/libc.so.6+0x24082)
    previously allocated by thread T0 here:
    #4 0x7f3fea5be082 (/lib/x86_64-linux-gnu/libc.so.6+0x24082)
    Shadow bytes around the buggy address:
    0x0c067fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0x0c067fff8000: fa fa fd fd fd fa fa fa fd fd fd fa fa fa fd fd
    0x0c067fff8010: fd fa fa fa 00 00 00 07 fa fa fd fd fd fa fa fa
    0x0c067fff8020: fd fd fd fa fa fa fd fd fd fa fa fa fd fd fd fa
    0x0c067fff8030: fa fa fd fd fd fa fa fa fd fd fd fa fa fa fd fd
    =>0x0c067fff8040: fd fa fa fa fd fd fd fa fa fa fd[fd]fd fa fa fa
    0x0c067fff8050: 00 00 00 fa fa fa fa fa fa fa fa fa fa fa fa fa
    0x0c067fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    0x0c067fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    0x0c067fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    0x0c067fff8090: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    Shadow byte legend (one shadow byte represents 8 application bytes):
    Addressable: 00
    Partially addressable: 01 02 03 04 05 06 07
    Heap left redzone: fa
    Freed heap region: fd
    Stack left redzone: f1
    Stack mid redzone: f2
    Stack right redzone: f3
    Stack after return: f5
    Stack use after scope: f8
    Global redzone: f9
    Global init order: f6
    Poisoned by user: f7
    Container overflow: fc
    Array cookie: ac
    Intra object redzone: bb
    ASan internal: fe
    Left alloca redzone: ca
    Right alloca redzone: cb
    Shadow gap:

    代码:

    class Solution {	//669. 修剪二叉搜索树
    public:
    	TreeNode* trimBST(TreeNode* root, int low, int high) {
    		if (root == nullptr) return root;
    		if (root->val < low) {
    			TreeNode* tmp = trimBST(root->right, low, high);
    			delete root;	//有问题,可以不加这句话,但是可能有内存泄漏问题
    			return tmp;
    		}
    		if (root->val > high) {
    			TreeNode* tmp = trimBST(root->left, low, high);
    			delete root;	//有问题,可以不加这句话,但是可能有内存泄漏问题
    			return tmp;
    		}
    
    		root->left = trimBST(root->left, low, high);
    		root->right = trimBST(root->right, low, high);
    		return root;
    	}
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    把delete root;去掉,不会再出现这个问题。

  • 相关阅读:
    【Django】开发日报_10_Day:手机号码管理系统(8)
    【C++实现】 数据库连接池
    pytes中fixture的scope: 决定可以在什么范围内共享fixture
    8.softmax回归
    【学习笔记】开源计算机视觉库OPENCV学习方案
    Go语学习笔记 - gorm使用 - 表增删改查 | Web框架Gin(八)
    MongoDB入门与实战-第五章-MongoDB副本集
    首设农作物种业专区农民丰收节国际贸易促进会舌尖上进博会
    wasm+pygbag让你在网页上也能运行Python代码:【贪吃蛇游戏】
    设计模式之外观模式
  • 原文地址:https://blog.csdn.net/weixin_42817333/article/details/125430900