Coverity reports leaks for the following code. I would like some help understanding the errors and to re-write this code to be error free. ( The errors are annotated as comments in the code below )
int main()
{
...
B* b = ...
// (1) Coverity: Storage is returned from
// allocation function operator new
// (2) Coverity: Assigning ...
A* a = new A();
// (3) Coverity: noescape: Resource a is not freed
// or pointed-to in add_a_to_b
b->add_a_to_b( *a );
...
// (4) Coverity: Resource leak: Variable a going out
// of scope leaks the storage it points to.
}
class B {
public:
std::vector<A> a_vector;
void add_a_to_b( const A& a )
{
a_vector.push_back( a );
}
-- EDIT ---
I had a particular question about the B::add_a_to_b function, and this reflects my incomplete understanding of references perhaps: Does a_vector store a reference to A or does it create a copy of the object passed to add_a_to_b?