valgrind在向量构造函数中显示内存泄漏

时间:2017-05-02 10:57:31

标签: c++ memory-leaks valgrind

我正在使用bison和C ++编写编译器,我刚用Valgrind检查了内存泄漏。虽然它显示了由下面显示的向量构造函数引起的内存泄漏:

==6562== HEAP SUMMARY:
==6562==     in use at exit: 90,298 bytes in 27 blocks
==6562==   total heap usage: 125 allocs, 98 frees, 100,782 bytes allocated
==6562== 
==6562== 80 (32 direct, 48 indirect) bytes in 1 blocks are definitely lost in loss record 19 of 22
==6562==    at 0x4C2E0EF: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6562==    by 0x40DB9B: __gnu_cxx::new_allocator<std::shared_ptr<Node> >::allocate(unsigned long, void const*) (new_allocator.h:104)
==6562==    by 0x40D3B9: std::allocator_traits<std::allocator<std::shared_ptr<Node> > >::allocate(std::allocator<std::shared_ptr<Node> >&, unsigned long) (alloc_traits.h:491)
==6562==    by 0x40C465: std::_Vector_base<std::shared_ptr<Node>, std::allocator<std::shared_ptr<Node> > >::_M_allocate(unsigned long) (stl_vector.h:170)
==6562==    by 0x41C170: std::_Vector_base<std::shared_ptr<Node>, std::allocator<std::shared_ptr<Node> > >::_M_create_storage(unsigned long) (stl_vector.h:185)
==6562==    by 0x41ADB6: std::_Vector_base<std::shared_ptr<Node>, std::allocator<std::shared_ptr<Node> > >::_Vector_base(unsigned long, std::allocator<std::shared_ptr<Node> > const&) (stl_vector.h:136)
==6562==    by 0x41A13F: std::vector<std::shared_ptr<Node>, std::allocator<std::shared_ptr<Node> > >::vector(std::vector<std::shared_ptr<Node>, std::allocator<std::shared_ptr<Node> > > const&) (stl_vector.h:320)
==6562==    by 0x417ECF: OprNode::OprNode(int, std::vector<std::shared_ptr<Node>, std::allocator<std::shared_ptr<Node> > > const&) (Node.cpp:52)
==6562==    by 0x406C7B: opr(int, std::vector<Node*, std::allocator<Node*> > const&) (parser.y:175)
==6562==    by 0x40555B: yyparse() (parser.y:125)
==6562==    by 0x40748B: main (parser.y:253)
==6562== 
==6562== LEAK SUMMARY:
==6562==    definitely lost: 32 bytes in 1 blocks
==6562==    indirectly lost: 48 bytes in 2 blocks
==6562==      possibly lost: 0 bytes in 0 blocks
==6562==    still reachable: 90,218 bytes in 24 blocks
==6562==         suppressed: 0 bytes in 0 blocks
==6562== Reachable blocks (those to which a pointer was found) are not shown.
==6562== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==6562== 
==6562== For counts of detected and suppressed errors, rerun with: -v
==6562== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

相关代码:

class Node {
public:
    Node()=default;
    virtual void ex(int, int, int) const = 0;
    virtual void check(vector<int>&, int) const = 0;
};

class OprNode:public Node {
public:
    OprNode(int, const vector<shared_ptr<Node>>&);
    void ex(int, int, int) const;
    void check(vector<int>&, int) const;
private:
    int oper;
    vector<shared_ptr<Node>> op;
};
OprNode::OprNode(int oper, const vector<shared_ptr<Node> >& op):oper(oper), op(op) {;}
Node * opr(int oper, const vector<Node *>& op) {
    auto v = vector<shared_ptr<Node> >();
    for (const auto i: op) {
        v.push_back(shared_ptr<Node>(i));
    }
    return new OprNode(oper, v);
}

由于bison的限制,我必须使用原始指针作为传递给opr的向量的返回类型和内容。

非常感谢!

0 个答案:

没有答案