已关闭
【缺陷报告】内存泄漏 - 文件test_aclnn_nsa_compress_grad.cpp - 函数main - 行号177 #4794
zhangjunkai9创建于  17 天前关闭于  3 天前
zhangjunkai9
17 天前 创建

缺陷信息

缺陷类型memoryLeak (内存泄漏)
函数main
文件attention/nsa_compress_grad/examples/test_aclnn_nsa_compress_grad.cpp
行号177
置信度90%
分析模型maas-glm-5.1-zhipu

缺陷描述

在 main 函数中,第177行通过 aclCreateIntArray 创建了 actSeqLenOptional 对象(aclIntArray 类型,动态分配)。但在函数末尾的资源释放段(第229-243行),仅释放了5个 aclTensor(aclDestroyTensor)和5个 device 地址(aclrtFree)以及 workspace,从未调用 aclDestroyIntArray(actSeqLenOptional) 释放该对象,导致 aclIntArray 内存泄漏。

事实核查

核查1(模式成立):第177行 aclIntArray *actSeqLenOptional = aclCreateIntArray(...) 动态创建 aclIntArray 对象;

第201行将其传入 aclnnNsaCompressGradGetWorkspaceSize 使用;

函数末尾释放段(第229-243行)仅调用 aclDestroyTensor 释放5个 aclTensor 和 aclrtFree 释放5个 device 地址及 workspace,从未调用 aclDestroyIntArray(actSeqLenOptional),泄漏模式成立。

核查2(防护无效):释放段无 aclDestroyIntArray 调用。

核查3(函数可达):main 为入口函数,depth 0,可达。

核查4(触发可满足):泄漏发生在成功路径即正常执行路径,程序正常执行到第248行 return 0 时 actSeqLenOptional 泄漏,触发条件满足。

模式成立+无防护+可达+触发可满足,确认为真实缺陷。

数据流证据

Source(问题源头)

attention/nsa_compress_grad/examples/test_aclnn_nsa_compress_grad.cpp:177 行 aclIntArray *actSeqLenOptional = aclCreateIntArray(actSeqLenOptionalHostData.data(), actSeqLenOptionalHostData.size()) 动态创建 aclIntArray 对象

Sink(问题爆发点)

attention/nsa_compress_grad/examples/test_aclnn_nsa_compress_grad.cpp:243 行 资源释放段结束(第229-243行),未包含 aclDestroyIntArray(actSeqLenOptional) 调用

传播路径:

# 文件 行号 说明
1 attention/nsa_compress_grad/examples/test_aclnn_nsa_compress_grad.cpp 177 aclIntArray *actSeqLenOptional = aclCreateIntArray(...) 动态分配 aclIntArray 对象,actSeqLenOptional 指针持有该对象
2 attention/nsa_compress_grad/examples/test_aclnn_nsa_compress_grad.cpp 201 aclnnNsaCompressGradGetWorkspaceSize(..., actSeqLenOptional, ...) 将 actSeqLenOptional 作为参数传入算子 API 使用
3 attention/nsa_compress_grad/examples/test_aclnn_nsa_compress_grad.cpp 229-233 aclDestroyTensor 释放5个 aclTensor,但不释放 aclIntArray 对象
4 attention/nsa_compress_grad/examples/test_aclnn_nsa_compress_grad.cpp 236-243 aclrtFree 释放5个 device 地址和 workspace,但不释放 aclIntArray 对象
5 attention/nsa_compress_grad/examples/test_aclnn_nsa_compress_grad.cpp 248 return 0 函数返回,actSeqLenOptional(第177行创建)从未被 aclDestroyIntArray 释放,造成内存泄漏

调用链

可达调用链1 起点:(entry functions) → 终点:main 深度:0

# 文件 函数 函数起始行 调用点行
1 attention/attention_update/examples/test_aclnn_attention_update.cpp main 77

修复建议

// 在第233行之后、aclrtFree 释放段之前添加 aclDestroyIntArray 调用
aclDestroyTensor(weightGradOut);

// 释放 aclIntArray 资源
aclDestroyIntArray(actSeqLenOptional);

// 7. 释放device资源
likedislike
weihao18成员
16 天前 评论:

你好,建议汇总到具体算子进行集中处理

likedislike
游震成员
14 天前 评论:

/assign @yu-xinjie62

likedislike
CANN-robotCANN-robot成员
14 天前 将 yu-xinjie62 设为负责人
memorecoolmemorecool成员
7 天前 将 hz36amy_00 设为负责人
yf_zhouyf_zhou成员
6 天前 关联了pull request:fix: release NSA example int arrays
CANN-robotCANN-robot成员
3 天前 关闭了 issue
CANN-robotCANN-robot成员
3 天前 添加了label:resolved