GCC软件内存使用问题定位方法

AddressSanitizer介绍

AddressSanitizer is a fast memory error detector. It consists of a compiler instrumentation module and a run-time library. The tool can detect the following types of bugs:

  • Out-of-bounds accesses to heap, stack and globals
  • Use-after-free
  • Use-after-return (runtime flag ASAN_OPTIONS=detect_stack_use_after_return=1)
  • Use-after-scope (clang flag -fsanitize-address-use-after-scope)
  • Double-free, invalid free
  • Memory leaks (experimental)

使用方法

在gcc编译选项中添加:-fsanitize=address
如在CMakeLists.txt中:set(CMAKE_C_FLAGS “-Wall -Werror -fsanitize=address”)

举例

如果发生上述问题,则运行程序后,系统会主动报告问题原因和代码位置:

int main(int argc, char const **argv)
{
    char *array = malloc(100);
    strcpy(array, "test");
    return 0;
}

程序运行后,系统会主动报告出现的问题和代码位置,如下图所示,最后一行显示上述代码发生了内存泄露,100个字节,因此可以断定是申请的array没有释放导致。
在这里插入图片描述
其他问题,比如内存越界,重复释放等,空指针访问,空指针释放等都可以容易定位。
https://github.com/google/sanitizers/wiki/AddressSanitizer

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星空嵌入式

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值