munmap_chunk():带有3D向量网格的无效指针错误

时间:2019-03-11 19:45:26

标签: c++ vector memory-leaks valgrind

在不赘述的情况下,我将尽量简洁。我正在编写的程序涉及创建3D网格。网格由多维数据集类对象组成,其中每个多维数据集都包含许多代表网格那部分的变量:

class Cube{
  public:
    double var1, var2, var3;

    Cube();
};

我使用向量在函数中生成网格:

class PlasmaGrid{
  public:
    vector<vector<vector<Cube>>> construct_grid();
    void assign_background(vector<vector<vector<Cube>>> &grid);
};

vector<vector<vector<Cube>>> PlasmaGrid::construct_grid(){

    vector<vector<vector<Cube>>> grid(input_file.num_rad_bins,
                                      vector<vector<Cube>>(input_file.num_pol_bins,
                                      vector<Cube>(input_file.num_par_bins, Cube())));

    return grid

void PlasmaGrid::assign_background(vector<vector<vector<Cube>>> &grid){

    // loop through the grid to fill in variables...
}

在以下各行中,从另一个文件中调用它们:

vector<vector<vector<Cube>>> grid;
grid = plasma_grid.construct_grid();
plasma_grid.assign_background(grid);

最后,

Cube get_my_cube(vector<vector<vector<Cube>>> &grid){

// Do some stuff that isn't relevant to the question...

return grid[index1][index2][index3];
}

Cube cube = get_my_cube(grid);

现在的问题是我运行时出现错误:

munmap_chunk(): invalid pointer
Aborted (core dumped)

我已经追溯到我返回grid [index1] [index2] [index3](即一个Cube对象)并将其分配给多维数据集的那一行。

代码确实在valgrind中运行完毕,尽管它似乎只是告诉我我有内存泄漏问题。我可以找到的有关内存泄漏的大多数信息都涉及到使用new和delete,但在此程序中我没有使用任何这些信息。希望这个问题不会太长。如果您认为需要解决该问题,我可以提供更多信息。

0 个答案:

没有答案