简体   繁体   English

Valgrind的输出是什么意思?

[英]What does this Valgrind output mean?

So I'm writing this C++ program and I've narrowed down my memory leaks by quite a bit thank to valgrind . 因此,我正在编写此C++程序,并且通过valgrind大大减少了我的内存泄漏。 But I still have a large chunk of leaks to go. 但是我还有很多漏洞要解决。 But my valgrind output isn't making any sense: 但是我的valgrind输出没有任何意义:

9,512 (7,104 direct, 2,408 indirect) bytes in 148 blocks are definitely lost in loss record 4 of 4

==2638==    at 0x402B9B4: operator new(unsigned int) (in /usr/lib/valgrind
/vgpreload_memcheck-x86-linux.so)
==2638==    by 0x804C8AD: __gnu_cxx::new_allocator<std::vector<plate, std::allocator<plate> > >::allocate(unsigned int, void const*) (in /home/.../program)
==2638==    by 0x804C2FF: std::_Vector_base<std::vector<plate, std::allocator<plate> >, std::allocator<std::vector<plate, std::allocator<plate> > > >::_M_allocate(unsigned int) (in /home/.../program)
==2638==    by 0x804B7F7: std::vector<std::vector<plate, std::allocator<plate> >, std::allocator<std::vector<plate, std::allocator<plate> > > >::_M_insert_aux(__gnu_cxx::__normal_iterator<std::vector<plate, std::allocator<plate> >*, std::vector<std::vector<plate, std::allocator<plate> >, std::allocator<std::vector<plate, std::allocator<plate> > > > >, std::vector<plate, std::allocator<plate> > const&) (in /home/.../program)
==2638==    by 0x804AF48: std::vector<std::vector<plate, std::allocator<plate> >, std::allocator<std::vector<plate, std::allocator<plate> > > >::push_back(std::vector<plate, std::allocator<plate> > const&) (in /home/.../program)
==2638==    by 0x804A57C: hanoi_object::hanoi_object(hanoi_object const&) (in /home/.../program)
==2638==    by 0x804A6C9: hanoi_object::getMoves() const (in /home/.../program)
==2638==    by 0x8048FE1: Solver::solve(std::Config*, std::Config*) (in /home/.../program)
==2638==    by 0x804A0D3: main (in /home/.../program)

Program abstract~ skip if you want. 程序摘要〜如果需要,请跳过。

My program is basically a rudimentary Hanoi puzzle solver using a breadth first search algorithm to solve. 我的程序基本上是基本的河内难题求解器,使用广度优先搜索算法进行求解。 Now, I represent the puzzle itself by a double vector of plate objects ( vector< vector< plate > > ), where plate is but a simple class that holds one integer value, the weight or size of the disk. 现在,我用板对象的双重向量( vector< vector< plate > > )来表示难题本身,其中plate只是一个简单的类,它具有一个整数值,即磁盘的重量或大小。 A class called hanoi_object is responsible for collecting, representing, moving and maintaining these objects. 名为hanoi_object的类负责收集,表示,移动和维护这些对象。 A solver class will later allocate various hanoi_objects on the heap and delete it if it's not what we're looking for. 稍后,求解器类将在堆上分配各种hanoi_objects ,如果不是我们想要的对象,则将其删除。

According to valgrind it looks like it's having issues with my double vector of plate objects. 根据valgrind说法,我的平板物体的双重向量似乎有问题。 But why? 但为什么? It's a class that contains a simple int value and I never allocate any plate objects on the heap. 这是一个包含简单int值的类,我从不在堆上分配任何板对象。 Does allocating the handler/controller object ( hanoi_object ) on the heap also allocate my double vector plate object or something? 在堆上分配处理程序/控制器对象( hanoi_object )是否还会分配双矢量板对象或其他对象?

Here's the destructor: 这是析构函数:

hanoi_object::~hanoi_object(){
    for(int i=0; i<pegs_.size(); i++){
        pegs_[i].clear();
    }
    pegs_.clear();
}

And the copy constructor: 和复制构造函数:

hanoi_object::hanoi_object(const hanoi_object& hanoi_object){
    for(int i = 0; i < hanoi_object.pegs_.size(); i++){
        vector<plate> PL;
        for(int j = 0; j < hanoi_object.pegs_[i].size(); j++){
            plate somePlate(hanoi_object.pegs_[i][j].getWeight());
            PL.push_back(somePlate);
        }
        pegs_.push_back(PL);
    }
}

All allocations in the hanoi_object are in other functions and I've triple checked to make sure that those are either deleted or passed to another class. hanoi_object中的所有分配都在其他函数中,并且我进行了三遍检查,以确保将其删除或传递给另一个类。

It appears that your copy constructor hanoi_object::hanoi_object(hanoi_object const&) is allocating something (probably with new ) which your destructor is not freeing (with delete ). 看来您的复制构造函数hanoi_object::hanoi_object(hanoi_object const&)正在分配一些析构函数无法释放的内容(可能带有new )(带有delete )。 It looks like it is the double vector of plate s. 它看起来像它的双载体plate秒。 Much more than that, we can't say without seeing the code for the copy constructor and the destructor. 不仅如此,我们不能不看复制构造函数和析构函数的代码。


The boundary between 'system' and 'user' functions in the trace is at: 跟踪中“系统”和“用户”功能之间的边界位于:

  • std::vector<std::vector<plate, std::allocator<plate> >, std::allocator<std::vector<plate, std::allocator<plate> > > > ::push_back(std::vector<plate, std::allocator<plate> > const&)
  • hanoi_object::hanoi_object(hanoi_object const&)

This indicates to me that one of the two push_back calls in the copy constructor is doing the damage. 这向我表明,复制构造函数中的两个push_back调用之一正在造成损害。 Given the type information, I think it is the second such call: 给定类型信息,我认为这是第二个此类调用:

pegs_.push_back(PL);

Formally, we can't tell from the information given whether pegs_ is a global variable or a class member (yes, I know; it is probably a class member, but we can't be sure since you've not shown us even an outline of the class definition). 从形式上pegs_ ,我们无法从给定的信息中分辨出pegs_是全局变量还是类成员(是的,我知道;它可能是类成员,但是我们无法确定,因为您甚至没有向我们展示类定义的概要)。

In the destructor, you clear the pegs_ variable. 在析构函数中, clear pegs_变量。 This removes and destroys all the elements in the vector; 这将删除并破坏向量中的所有元素; it does not free the vector itself. 它不会释放向量本身。

I never allocate any plate objects on the heap. 我从不在堆上分配任何板对象。

vector allocates on the heap. 向量在堆上分配。

Just trust valgrind. 只要相信 valgrind。 The line he complaining about allocates memory which was never freed. 他抱怨的那一行分配了从未释放的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM