已合并
fix: 修复examples中多个算子的内存泄漏及缓冲区越界问题 #1261
liu-wei创建于 10 天前
fix: 修复examples中多个算子的内存泄漏及缓冲区越界问题 #1261
已合并
liu-wei创建于 10 天前
liu-wei
liu-wei成员
10 天前

Closes #719, #723, #724, #725, #731

描述

修复多个算子的内存泄漏及缓冲区越界问题,涉及 example 文件、kernel、aicpu 算子、UT 框架共 24 个文件。

修复分类

类别 文件数 问题
内存泄漏 18 new[] 分配后未 delete[],Tensor 构造后 buffer 泄漏
缓冲区越界 1 nms_with_mask.hsizeof(T) 应为 sizeof(uint8_t),float 类型下读取 4 倍数据
错误处理缺失 1 scale_and_translate_aicpu.cpp:spans 分配/计算失败后未释放已分配内存、返回值未检查
空指针保护 1 c_shell.cpprealpath 返回值未判空
UT 内存泄漏 1 tensor_desc.cppts 数组 + tensor 未释放

关键修复

文件 问题 修复
nms_with_mask.h scoresLength * sizeof(T) 当 T=float 时越界读取 4 倍数据 改为 sizeof(uint8_t)
scale_and_translate_aicpu.cpp spans 分配失败后泄漏 + 返回值不检查 每条错误路径补充 cleanup;SWITCH_PARALLEL 改为显式分支并检查返回值
17 个 example 文件 new[] 分配后 Tensor 构造完未 delete[] 补充释放
c_shell.cpp realpath(NULL) 返回 NULL 时 crash 判空 + free
tensor_desc.cpp ts 数组 + tensor 泄漏 补充释放逻辑

功能影响

正常执行路径零影响,异常路径从静默吞错改为正确返回错误码。

关联的Issue

  • #719
  • #723
  • #724
  • #725
  • #731

测试

相关功能点都已经经过测试。

文档更新

op_list.md 增加说明。

类型标签

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 liu-wei 的贡献)
liu-weiliu-wei成员
10 天前 创建了 pull request,commit 4170760d
atomgit-bot
atomgit-bot
10 天前 评论:

变更摘要

此 PR 主要修复了多个算子中的内存泄漏、缓冲区越界及资源清理问题。涉及约 20 个算子测试文件中 GenOnesData / GenOnesDataFloat32 辅助函数使用 new[] 分配内存后未释放的问题,以及 NMSWithMask 内核中 DataCopyExtParams 使用了错误的数据类型大小计算导致的缓冲区越界。同时修复了 scale_and_translate_aicpu 中资源分配失败时未回滚已分配内存、tensor_desc.cppts 数组泄漏、以及 c_shell.cpprealpath 返回值未校验的问题。

主要改动

  • 测试辅助函数内存泄漏修复: 在 examples/add_exampleimage/objdetect/ 等目录下共约 20 个测试文件中的 GenOnesDataGenOnesDataFloat32 函数内,Tensor 构造完成后补充了 delete[] pData,释放通过 new[] 分配的临时缓冲区。
  • NMSWithMask::CopyOut 缓冲区大小修复: 将 experimental/image/nms_with_mask/op_kernel/nms_with_mask.hDataCopyExtParams 的大小参数从 scoresLength * sizeof(T) 修正为 scoresLength * sizeof(uint8_t),与目标 LocalTensor<uint8_t> 类型匹配,避免越界。
  • InitSpans 资源回滚修复: 在 image/scale_and_translate/op_kernel_aicpu/scale_and_translate_aicpu.cpp 中,当 spans->weights 分配失败时,先 delete spans->starts 并置空后再返回错误码,避免已分配内存泄漏。
  • GetExePathrealpath 返回值校验: 在 tests/ut/op_api/op_api_ut_common/src/inner/c_shell.cpp 中,对 realpath 返回值增加了空指针检查,并在使用后调用 free 释放,避免内存泄漏和空指针解引用。
  • TensorListDesc::ToAclTypeRawPtr 临时数组泄漏修复: 在 tests/ut/op_api/op_api_ut_common/src/tensor_desc.cpp 中,aclCreateTensorList 调用后补充了 delete[] ts,释放用于传递张量指针的临时数组。
likedislike
atomgit-bot
atomgit-bot
10 天前 评论:

代码审查

✅ 未发现问题

likedislike
CANN-robotCANN-robot成员
10 天前 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
10 天前 评论:

CLA Signature Pass

liu-wei, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
此处折叠了144条消息 查看更多
liu-weiliu-wei成员
9 天前 删除了关联的issue:[Bug-Report|缺陷反馈]: 内存泄漏 - tensor_desc.cpp
liu-weiliu-wei成员
9 天前 删除了关联的issue:[Bug-Report|缺陷反馈]: 内存泄漏 - c_shell.cpp
liu-weiliu-wei成员
9 天前 删除了关联的issue:[Bug-Report|缺陷反馈]: 内存泄漏 - test_geir_add_example.cpp
liu-weiliu-wei成员
9 天前 删除了关联的issue:[Bug-Report|缺陷反馈]: 内存操作函数的目的缓冲区访问长度设置不正确 - nms_with_mask.h
liu-weiliu-wei成员
9 天前 关联了issue:[Documentation|文档反馈]: 文档需补充符号说明的描述信息