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));
…
}
…..
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…