Loading... >> 使用关键字快速定位日志查看问题 >> > > # 一、什么是内存泄漏 > > ``` > 内存泄漏是指由于疏忽或错误造成程序未能释放已经不再使用的内存。内存泄漏并非指内存在物理上的消失,而是应用程序分配某段内存后,由于设计错误,导致在释放该段内存之前就失去了对该段内存的控制,从而造成了内存的浪费。 > ``` > # 二、ASan > > 推荐新的一个排查内存泄漏的工具:AddressSanitizer(ASan),该工具为gcc自带,4.8以上版本都可以使用,支持Linux、OS、Android等多种平台,不止可以检测内存泄漏,它其实是一个内存错误检测工具,可以检测的问题有: > > * 内存泄漏 > * 堆栈和全局内存越界访问 > * free后继续使用 > * 局部内存被外层使用 > * Initialization order bugs > > 具体使用也可以参考官方文档:[https://github.com/google/sanitizers/wiki/AddressSanitizer](https://github.com/google/sanitizers/wiki/AddressSanitizer) > > ```cpp > AddressSanitizer (aka ASan) is a memory error detector for C/C++. It finds: > Use after free (dangling pointer dereference) > Heap buffer overflow > Stack buffer overflow > Global buffer overflow > Use after return > Use after scope > Initialization order bugs > Memory leaks > ``` > ## 2.1、Memory leaks - 内存泄漏 > >> 只有跑到了那段代码才会把内存泄漏打印出来 >> > > ```cpp > #include <iostream> > #include <vector> > #include <string> > > class LeakyClass { > public: > LeakyClass(size_t size) { > data = new int[size]; // 动态分配内存 > this->size = size; > std::cout << "LeakyClass created with size " << size << std::endl; > } > > void fillData() { > for (size_t i = 0; i < size; ++i) { > data[i] = static_cast<int>(i); > } > } > > void printData() { > for (size_t i = 0; i < size; ++i) { > std::cout << data[i] << " "; > } > std::cout << std::endl; > } > > // 注意:没有析构函数释放data,导致内存泄漏 > > private: > int* data; > size_t size; > }; > > void createLeak() { > LeakyClass* leak1 = new LeakyClass(1000); > leak1->fillData(); > leak1->printData(); > // 没有delete,内存泄漏 > > std::vector<LeakyClass*> leaks; > for (int i = 0; i < 5; ++i) { > LeakyClass* leak = new LeakyClass(500 * (i + 1)); > leak->fillData(); > leaks.push_back(leak); > } > // 这些对象也没有delete,造成多处内存泄漏 > } > > int main() { > createLeak(); > std::cout << "Program finished.\\n"; > return 0; > } > ``` > 执行完之后打印信息: > > ```cpp > peter@iZbp1est4y0mdt6ask7pgpZ:~/memleak$ g++ -fsanitize=address -g memleak.cpp -o memleak && ./memleak > LeakyClass created with size 1000 > 0 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 > LeakyClass created with size 500 > LeakyClass created with size 1000 > LeakyClass created with size 1500 > LeakyClass created with size 2000 > LeakyClass created with size 2500 > Program finished. > > ================================================================= > ==330738==ERROR: LeakSanitizer: detected memory leaks > > Direct leak of 80 byte(s) in 5 object(s) allocated from: > #0 0x7f1eb688c947 in operator new(unsigned long) (/lib/x86_64-linux-gnu/libasan.so.5+0x10f947) > #1 0x555fab85564f in createLeak() /home/peter/memleak/memleak.cpp:41 > #2 0x555fab8557cb in main /home/peter/memleak/memleak.cpp:49 > #3 0x7f1eb63b3082 in __libc_start_main ../csu/libc-start.c:308 > > Direct leak of 16 byte(s) in 1 object(s) allocated from: > #0 0x7f1eb688c947 in operator new(unsigned long) (/lib/x86_64-linux-gnu/libasan.so.5+0x10f947) > #1 0x555fab8555dc in createLeak() /home/peter/memleak/memleak.cpp:34 > #2 0x555fab8557cb in main /home/peter/memleak/memleak.cpp:49 > #3 0x7f1eb63b3082 in __libc_start_main ../csu/libc-start.c:308 > > Indirect leak of 30000 byte(s) in 5 object(s) allocated from: > #0 0x7f1eb688cb47 in operator new[](unsigned long) (/lib/x86_64-linux-gnu/libasan.so.5+0x10fb47) > #1 0x555fab8558e6 in LeakyClass::LeakyClass(unsigned long) /home/peter/memleak/memleak.cpp:8 > #2 0x555fab85566e in createLeak() /home/peter/memleak/memleak.cpp:41 > #3 0x555fab8557cb in main /home/peter/memleak/memleak.cpp:49 > #4 0x7f1eb63b3082 in __libc_start_main ../csu/libc-start.c:308 > > Indirect leak of 4000 byte(s) in 1 object(s) allocated from: > #0 0x7f1eb688cb47 in operator new[](unsigned long) (/lib/x86_64-linux-gnu/libasan.so.5+0x10fb47) > #1 0x555fab8558e6 in LeakyClass::LeakyClass(unsigned long) /home/peter/memleak/memleak.cpp:8 > #2 0x555fab8555ec in createLeak() /home/peter/memleak/memleak.cpp:34 > #3 0x555fab8557cb in main /home/peter/memleak/memleak.cpp:49 > #4 0x7f1eb63b3082 in __libc_start_main ../csu/libc-start.c:308 > > SUMMARY: AddressSanitizer: 34096 byte(s) leaked in 12 allocation(s). > > ``` > 编译方式很简单,只需要添加-fsanitize=address -g就可以检测出具体产生内存泄漏的位置以及泄漏空间的大小。 > > ## 2.2、Use after free - **free后被使用** > > ```cpp > #include <iostream> > > int main() { > int *array = new int[100]; > delete[] array; > int a = array[0]; // error 第六行 > return 0; > } > ``` > ```cpp > peter@iZbp1est4y0mdt6ask7pgpZ:~/memleak$ g++ -fsanitize=address -g useafterfree.cpp -o useafterfree && ./useafterfree > ================================================================= > ==330908==ERROR: AddressSanitizer: heap-use-after-free on address 0x614000000040 at pc 0x55dee3d402ed bp 0x7fff8d600f00 sp 0x7fff8d600ef0 > READ of size 4 at 0x614000000040 thread T0 > #0 0x55dee3d402ec in main /home/peter/memleak/useafterfree.cpp:6 > #1 0x7f32d9c77082 in __libc_start_main ../csu/libc-start.c:308 > #2 0x55dee3d401cd in _start (/home/peter/memleak/useafterfree+0x11cd) > > 0x614000000040 is located 0 bytes inside of 400-byte region [0x614000000040,0x6140000001d0) > freed by thread T0 here: > #0 0x7f32da136aaf in operator delete[](void*) (/lib/x86_64-linux-gnu/libasan.so.5+0x110aaf) > #1 0x55dee3d402b5 in main /home/peter/memleak/useafterfree.cpp:5 > #2 0x7f32d9c77082 in __libc_start_main ../csu/libc-start.c:308 > > previously allocated by thread T0 here: > #0 0x7f32da135b47 in operator new[](unsigned long) (/lib/x86_64-linux-gnu/libasan.so.5+0x10fb47) > #1 0x55dee3d4029e in main /home/peter/memleak/useafterfree.cpp:4 > #2 0x7f32d9c77082 in __libc_start_main ../csu/libc-start.c:308 > > SUMMARY: AddressSanitizer: heap-use-after-free /home/peter/memleak/useafterfree.cpp:6 in main > Shadow bytes around the buggy address: > 0x0c287fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 > 0x0c287fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 > 0x0c287fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 > 0x0c287fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 > 0x0c287fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 > =>0x0c287fff8000: fa fa fa fa fa fa fa fa[fd]fd fd fd fd fd fd fd > 0x0c287fff8010: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd > 0x0c287fff8020: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd > 0x0c287fff8030: fd fd fd fd fd fd fd fd fd fd fa fa fa fa fa fa > 0x0c287fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa > 0x0c287fff8050: 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: cc > ==330908==ABORTING > > ``` > 提示第六行代码有问题: > > #0 0x55dee3d402ec in main /home/peter/memleak/useafterfree.cpp:6 > > ## 2.3、Heap buffer overflow > >> 使用堆溢出,会导致coredump >> > > ```cpp > > #include <iostream> > > int main() { > int *array = new int[100]; > array[0] = 0; > int res = array[100]; // out of bounds 第七行 > delete[] array; > return res; > } > ``` > ```cpp > peter@iZbp1est4y0mdt6ask7pgpZ:~/memleak$ g++ -fsanitize=address -g headbufferoverflow.cpp -o headbufferoverflow && ./headbufferoverflow > ================================================================= > ==331087==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6140000001d0 at pc 0x55dda88d6341 bp 0x7ffd89871a30 sp 0x7ffd89871a20 > READ of size 4 at 0x6140000001d0 thread T0 > #0 0x55dda88d6340 in main /home/peter/memleak/headbufferoverflow.cpp:7 > #1 0x7f6d012a1082 in __libc_start_main ../csu/libc-start.c:308 > #2 0x55dda88d61ed in _start (/home/peter/memleak/headbufferoverflow+0x11ed) > > 0x6140000001d0 is located 0 bytes to the right of 400-byte region [0x614000000040,0x6140000001d0) > allocated by thread T0 here: > #0 0x7f6d0175fb47 in operator new[](unsigned long) (/lib/x86_64-linux-gnu/libasan.so.5+0x10fb47) > #1 0x55dda88d62be in main /home/peter/memleak/headbufferoverflow.cpp:5 > #2 0x7f6d012a1082 in __libc_start_main ../csu/libc-start.c:308 > > SUMMARY: AddressSanitizer: heap-buffer-overflow /home/peter/memleak/headbufferoverflow.cpp:7 in main > Shadow bytes around the buggy address: > 0x0c287fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 > 0x0c287fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 > 0x0c287fff8000: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00 > 0x0c287fff8010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 > 0x0c287fff8020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 > =>0x0c287fff8030: 00 00 00 00 00 00 00 00 00 00[fa]fa fa fa fa fa > 0x0c287fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa > 0x0c287fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa > 0x0c287fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa > 0x0c287fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa > 0x0c287fff8080: 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: cc > ==331087==ABORTING > > ``` > ## 2.4、Use after scope > > ```cpp > #include <iostream> > > int *p = 0; > > int main() { > { > int x = 0; > p = &x; > } > *p = 5; // 第十行 > return 0; > } > ``` > ```cpp > peter@iZbp1est4y0mdt6ask7pgpZ:~/memleak$ g++ -fsanitize=address -g useafterscope.cpp -o useafterscope && ./useafterscope > ================================================================= > ==331169==ERROR: AddressSanitizer: stack-use-after-scope on address 0x7ffd335655b0 at pc 0x55dff79653a1 bp 0x7ffd33565580 sp 0x7ffd33565570 > WRITE of size 4 at 0x7ffd335655b0 thread T0 > #0 0x55dff79653a0 in main /home/peter/memleak/useafterscope.cpp:10 > #1 0x7f7bdfbd5082 in __libc_start_main ../csu/libc-start.c:308 > #2 0x55dff79651cd in _start (/home/peter/memleak/useafterscope+0x11cd) > > Address 0x7ffd335655b0 is located in stack of thread T0 at offset 32 in frame > #0 0x55dff7965298 in main /home/peter/memleak/useafterscope.cpp:5 > > This frame has 1 object(s): > [32, 36) 'x' (line 7) <== Memory access at offset 32 is inside this variable > HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork > (longjmp and C++ exceptions *are* supported) > SUMMARY: AddressSanitizer: stack-use-after-scope /home/peter/memleak/useafterscope.cpp:10 in main > Shadow bytes around the buggy address: > 0x1000266a4a60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 > 0x1000266a4a70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 > 0x1000266a4a80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 > 0x1000266a4a90: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 > 0x1000266a4aa0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 > =>0x1000266a4ab0: 00 00 f1 f1 f1 f1[f8]f3 f3 f3 00 00 00 00 00 00 > 0x1000266a4ac0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 > 0x1000266a4ad0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 > 0x1000266a4ae0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 > 0x1000266a4af0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 > 0x1000266a4b00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 > 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: cc > ==331169==ABORTING > > ``` > 1 最后修改:2025 年 07 月 14 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏