C++ 内存泄漏在哪里?二维数组类

C++ 内存泄漏在哪里?二维数组类,c++,memory-management,memory-leaks,heap-memory,minesweeper,C++,Memory Management,Memory Leaks,Heap Memory,Minesweeper,我提交了一个课程的程序,错误消息说有一些内存泄漏,但我找不到(我甚至问了另一位教授) 以下是错误消息: ==27796== HEAP SUMMARY: ==27796== in use at exit: 160 bytes in 2 blocks ==27796== total heap usage: 192 allocs, 190 frees, 21,989 bytes allocated ==27796== ==27796== 160 bytes in 2 blocks are

我提交了一个课程的程序,错误消息说有一些内存泄漏,但我找不到(我甚至问了另一位教授)

以下是错误消息:

==27796== HEAP SUMMARY:
==27796==     in use at exit: 160 bytes in 2 blocks
==27796==   total heap usage: 192 allocs, 190 frees, 21,989 bytes allocated
==27796== 
==27796== 160 bytes in 2 blocks are definitely lost in loss record 1 of 1
==27796==    at 0x402ADFC: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==27796==    by 0x804D5C2: SweeperGrid::SweeperGrid(SweeperGrid const&) (in /tmp/grading20151030-8173-zfd6af-0/sweepertest)
==27796==    by 0x804BF57: main (sweepertest.cpp:357)
以下是我使用new或delete的任何代码:

//显式值构造函数

 SweeperGrid::SweeperGrid(const int initialRows, const int initialCols, const int density){
    if ((initialRows<5 || initialCols<5) || (density<25 || density>75)) {
        throw out_of_range("Grid not large enough (number of rows or columns cannot be fewer than 5) or density is too low or high (must be between 25% and 75%)");
    }

numRows = initialRows;
numColumns = initialCols;
numBombs = 0;

grid = new SweeperCell*[numRows];
for(int i=0; i <numRows; i++){
    grid[i] = new SweeperCell[numColumns];
}

srand(time(0));
for(int i=0; i<numRows; i++){
    for (int j=0; j<numColumns; j++){
        if(rand()%100+1<density){
            PlaceBomb(i, j);
        }
    }
}
}
SweeperGrid::SweeperGrid(SweeperGrid const & source){
    numRows=source.GetRows();
    numColumns=source.GetColumns();
    numBombs=source.GetBombs();

grid = new SweeperCell * [numRows];
for(int i=0; i < numRows; i++){
    grid[i] = new SweeperCell[numColumns];
}

for(int i=0; i<numRows; i++){
    for (int j=0; j<numColumns; j++){
        grid[i][j] = source.At(i, j);
    }
}
}
SweeperGrid::SweeperGrid(常量int initialRows,常量int initialCols,常量int density){

如果((initialRows您的问题是由于删除的行数与分配的行数不同所致:

void SweeperGrid::operator= (SweeperGrid const & source){
    numRows=source.GetRows(); // numRows becomes the assigned number of rows here.
                              // This is a problem, because now you don't remember how many
                              // rows you need to delete!
    numColumns=source.GetColumns();
    numBombs=source.GetBombs();

    for(int i=0; i<numRows; i++){ // you delete rows here, but the number of rows found in
                                  // the NEW grid, not the old one.
        delete [] grid[i];
    }
    delete [] grid;

...
}

也请看类定义。都德只需使用向量,就不会泄漏内存。在赋值运算符中,您删除的是指定网格中的行数,而不是上一个网格中的行数。我知道这不是最好的方法,但教授让我们这样完成项目。谢谢您的帮助!
void SweeperGrid::operator= (SweeperGrid const & source){
    numRows=source.GetRows();
    numColumns=source.GetColumns();
    numBombs=source.GetBombs();

for(int i=0; i<numRows; i++){
    delete [] grid[i];
}
delete [] grid;

grid = new SweeperCell * [numRows];
for(int i=0; i < numRows; i++){
    grid[i] = new SweeperCell[numColumns];
}

for(int i=0; i<numRows; i++){
    for (int j=0; j<numColumns; j++){
        grid[i][j] = source.At(i, j);
    }
}
}
void SweeperGrid::operator= (SweeperGrid const & source){
    numRows=source.GetRows(); // numRows becomes the assigned number of rows here.
                              // This is a problem, because now you don't remember how many
                              // rows you need to delete!
    numColumns=source.GetColumns();
    numBombs=source.GetBombs();

    for(int i=0; i<numRows; i++){ // you delete rows here, but the number of rows found in
                                  // the NEW grid, not the old one.
        delete [] grid[i];
    }
    delete [] grid;

...
}
void SweeperGrid::operator= (SweeperGrid const & source){

    for(int i=0; i<numRows; i++){ // you delete rows here, while you still remember 
                                  // how many you should delete
        delete [] grid[i];
    }
    delete [] grid;
    numRows=source.GetRows(); // numRows becomes the assigned number of rows here.
                              // This is not a problem, because now you don't need to
                              // remember how many rows you need to delete.
    numColumns=source.GetColumns();
    numBombs=source.GetBombs();
...
}
void SweeperGrid::operator= (SweeperGrid const & source){
    numRows=source.GetRows(); 
    numColumns=source.GetColumns();
    numBombs=source.GetBombs();

    grid = source.grid;
}