Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

memory leak in push function

This was my initial implementation, there is no memory leak detected when I call push function.

Class PQ
    {
                …
    Struct NodeData
    {
                …
                std::vector<double> nScores; 
                …          
    }
    std::set<NodeData> NodeSet;//NodeSet is a class property defined as a set of NodeData

    …
    Push()
    {           …
           double data[] = {1,2,3}; 
           NodeSet node;
           for (i=0;i<3;i++) { 
               node.nScores.push_back(data[i]);
           } 
           NodeSet.insert(std::move(node)); 
                            …
    }

…..
}

I have modified above code to dynamically allocate memory in push function as below. When I memory test the push function alone, it is leaking memory. Is this a valid memory leak.

Class PQ
    {
                …
    Struct NodeData
    {
                …
                unique_ptr<double[]> nScores; 
                …          
    }
    std::set<NodeData> NodeSet;//NodeSet is a class property defined as a set of NodeData

    …
    Push()
    {           …
           NodeSet node;
           node.nScores =std::make_unique<double[]>(3);
           NodeSet.insert(std::move(node)); 
                            …
    }

…..
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...