网站开发技术期末考试 及答案,阿里云自助建站和华为云自助建站,深圳金鼎网站建设,想要网站导航推广页内存泄漏是指程序动态分配的内存未能及时释放#xff0c;导致系统内存逐渐耗尽#xff0c;最终可能造成程序崩溃或性能下降。在C中#xff0c;内存泄漏通常发生在使用new或malloc等分配内存的操作时#xff0c;但没有正确地使用delete或free来释放这块内存。 在日常开发过程… 内存泄漏是指程序动态分配的内存未能及时释放导致系统内存逐渐耗尽最终可能造成程序崩溃或性能下降。在C中内存泄漏通常发生在使用new或malloc等分配内存的操作时但没有正确地使用delete或free来释放这块内存。 在日常开发过程中为了避免内存泄露一般都用智能指针去自动管理内存避免忘记释放。 1.内存泄露动态分析工具——Valgrind
Valgrind是运行在linux上的程序分析工具它包含很多小工具 memcheck(内存泄露检查工具)等
1.1 安装Valgrind
下载链接https://valgrind.org/downloads/current.html#current
1.2 Valgrind简单上手和分析
参考:Linux 性能分析valgrind一之memcheck使用
命令(以下程序均可以使用此命令)
valgrind --log-filevalgrind.log --toolmemcheck --leak-checkfull --show-leak-kindsall ./your_program
# --log-file: 报告文件名。如果没有指定输出到stderr
# --toolmemcheck: 指定Valgrind使用的工具Valgrind是一个工具集包括Memcheck、Cachegrind、Callgrind等多个工具memcheck是缺省项。
# --leak-check: 指定如何报告内存泄漏memcheck能检查多种内存使用错误内存泄漏是其中常见的一种可选值有:
# - no 不报告
# - summary 显示简要信息有多少个内存泄漏。summary是缺省值。
# - yes 和 full 显示每个泄漏的内存在哪里分配。
# --show-leak-kinds: 指定显示内存泄漏的类型的组合。类型包括definite, indirect, possible,reachable。也可以指定all或none。[缺省值](https://www.zhihu.com/search?q缺省值search_sourceEntityhybrid_search_sourceEntityhybrid_search_extra{sourceType%3Aarticle%2CsourceId%3A92074597})是definite,possible。 运行一段时间后想停止进程不要kill掉需要ctrl c来结束输出的log会在上述命令中的valgrind.log中。程序1C程序使用未初始化的内存
#include stdio.h
#include stdlib.h int main(void)
{char *p; char c *p; printf(\n [%c]\n,c); return 0;
}Valgrind重点结果信息使用未初始化的变量无效的读( 读取没有分配地址空间的区域数据 )
73374 Use of uninitialised value of size 8
73374 at 0x400513: main (in /home/bossdog/3Growup/valgrind/test1/a.exe)
73374
73374 Invalid read of size 1
73374 at 0x400513: main (in /home/bossdog/3Growup/valgrind/test1/a.exe)
73374 Address 0x0 is not stackd, mallocd or (recently) freed程序2C程序在内存被释放后进行读/写
#include stdio.h
#include stdlib.h int main(void)
{char *p malloc(1);*p a; char c *p; printf(\n [%c]\n,c); free(p);c *p;return 0;
}Valgrind重点结果信息:
74181 Invalid read of size 1
74181 at 0x4005E3: main (in /home/bossdog/3Growup/valgrind/test1/a.exe)
74181 Address 0x520a040 is 0 bytes inside a block of size 1 freed
74181 at 0x4C3195F: free (vg_replace_malloc.c:872)
74181 by 0x4005DE: main (in /home/bossdog/3Growup/valgrind/test1/a.exe)
74181 Block was allocd at
74181 at 0x4C2F075: malloc (vg_replace_malloc.c:381)
74181 by 0x4005A8: main (in /home/bossdog/3Growup/valgrind/test1/a.exe)程序3C程序: 内存泄露
#include stdio.h
#include stdlib.h int main(void)
{char *p malloc(1);*p a; char c *p; printf(\n [%c]\n,c); return 0;
}Valgrind重点结果信息直接泄露
74814 1 bytes in 1 blocks are definitely lost in loss record 1 of 1
74814 at 0x4C2F075: malloc (vg_replace_malloc.c:381)
74814 by 0x400558: main (in /home/bossdog/3Growup/valgrind/test1/a.exe)程序4C程序不匹配使用malloc free 和 new delete
#include stdio.h
#include stdlib.h
#includeiostream int main(void)
{char *p (char*)malloc(1);*p a; char c *p; printf(\n [%c]\n,c);delete p;return 0;
}Valgrind重点结果信息:
75341 by 0x400683: main (in /home/bossdog/3Growup/valgrind/test1/a.exe)
75341 Address 0x5b20c80 is 0 bytes inside a block of size 1 allocd
75341 at 0x4C2F075: malloc (vg_replace_malloc.c:381)
75341 by 0x400648: main (in /home/bossdog/3Growup/valgrind/test1/a.exe)程序5C程序 两次释放内存
#include stdio.h
#include stdlib.h int main(void)
{char *p (char*)malloc(1);*p a; char c *p;printf(\n [%c]\n,c);free(p);free(p);return 0;
}Valgrind重点结果信息:
76126 Invalid free() / delete / delete[] / realloc()
76126 at 0x4C3195F: free (vg_replace_malloc.c:872)
76126 by 0x4005EA: main (in /home/bossdog/3Growup/valgrind/test1/a.exe)
76126 Address 0x520a040 is 0 bytes inside a block of size 1 freed
76126 at 0x4C3195F: free (vg_replace_malloc.c:872)
76126 by 0x4005DE: main (in /home/bossdog/3Growup/valgrind/test1/a.exe)
76126 Block was allocd at
76126 at 0x4C2F075: malloc (vg_replace_malloc.c:381)
76126 by 0x4005A8: main (in /home/bossdog/3Growup/valgrind/test1/a.exe)1.3 官方手册
官方手册
2.实际问题分析
struct image_u8
{const int32_t width;const int32_t height;const int32_t stride;uint8_t *buf;
};std::shared_ptrimage_u8 image;image.reset(new image_u8({ frame_out.stFrameInfo.nWidth,frame_out.stFrameInfo.nHeight,frame_out.stFrameInfo.nWidth,new uint8_t[frame_out.stFrameInfo.nWidth * frame_out.stFrameInfo.nHeight * sizeof(uint8_t)] }));image.reset();std::shared_ptr 会自动管理 image_u8对象的生命周期但是它不会管理 buf即 uint8_t* 类型的指针所指向的内存。 当执行image.reset();之后image指针被释放重置了但是指针所指向的指针即 uint8_t*没有被释放造成了内存泄露。 记录
二级指针被释放时不会自动释放一级指针中所指向的内存。 参考https://blog.csdn.net/weixin_44477424/article/details/136417250