c++内存泄漏解释分析

C++ Memory Leak - interpreting analysis

本文关键字:解释 泄漏 内存 c++      更新时间:2023-10-16

下面这段c++代码有内存泄漏:

void onNewDepthSample(DepthNode node, DepthNode::NewSampleReceivedData data)
{
    INT32 w, h;
    FrameFormat_toResolution(data.captureConfiguration.frameFormat, &w, &h);
    INT32 size = data.depthMap.size();
    int16_t* ptr = new int16_t[size];
        for (int i = 0; i < size; i++){
            ptr[i] = data.depthMap[i];
        }
    depthCB(stringToCsharpString(node.getSerialNumber()), &w, &h, ptr);
    delete[] ptr;   
}

这个方法每秒被调用30次。内存泄漏分析:

Function details
Function   DepthsenseDriver!malloc+49 
Source Line    
Allocation type   Heap allocation(s) 
Heap handle   0xcf11e4ac 
Allocation Count   75256 allocation(s) 
Allocation Size   1,22 MBytes 
Leak Probability   100%

Call stack sample 1
Address   0x32335344 
Allocation Time   00:05:00 since tracking started 
Allocation Size   17 Bytes 

Call stack:
DepthsenseDriver!malloc+49   
DepthsenseDriver!operator new+1d
DepthsenseDriver!CreateFileW+96b    
DepthsenseDriver!DepthSense::FunctionHandler<DepthSense::DepthNode,DepthSense::DepthNode::NewSampleReceivedData>::operator()+5d  

我想知道我该如何解释这个分析。例如,有一个CreateFileW函数调用,但在我的代码中,这个函数调用不存在。

我是一个c++新手,所以任何建议或改进都是欢迎的:)

使用Deleaker,我发现是stringToCsharpString方法导致了泄漏。