Coverity 报告以下代码的泄漏。我需要一些帮助来理解错误并重新编写此代码以确保没有错误。(错误在下面的代码中被注释为注释)
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 );
}
- 编辑 - -
我对 B::add_a_to_b 函数有一个特殊的问题,这可能反映了我对引用的不完整理解:a_vector 是存储对 A 的引用还是创建传递给 add_a_to_b 的对象的副本?