这些 valgrind/GNU 调试器错误消息是什么意思

What do these valgrind/GNU debugger error messages mean?

本文关键字:消息 是什么 意思 错误 调试器 valgrind GNU 这些      更新时间:2023-10-16

我的程序正确终止,根据Valgrind的说法,没有泄漏的内存。

但是,首次运行对象方法时,会出现以下消息。

Use of uninitialised value of size 8

获取有关上述内容的更多详细信息

==18787== Use of uninitialised value of size 8
==18787==    at 0x4017F3: Grid::init(int) (grid.cc:90)
==18787==    by 0x401D2A: main (main.cc:43)
==18787==  Uninitialised value was created by a stack allocation
==18787==    at 0x401BE4: main (main.cc:11)

我还看到以下消息:

Conditional jump or move depends on uninitialised value(s)
Invalid free() / delete / delete[] / realloc()
Address 0x7ff000b08 is on thread 1's stack

这是网格的代码::init

void Grid::init(int n){
    if (!(&(this->theGrid))) { this->clearGrid(); } //If non-empty, destroys grid
    this->theGrid = new Cell*[n]; //Create new grid of size n x n
    this->td = new TextDisplay(n); //new Text Display 
    for (int i = 0; i < n; i++){
        this->theGrid[i] = new Cell[n];
        for(int j = 0; j < n; j++){ //Cell initializations
            (this->theGrid[i][j]).setDisplay(this->td); //set display
            (this->theGrid[i][j]).setCoords(i, j); //set co-ordinates
            (this->theGrid[i][j]).setState(0); //default state
        }
    }
}

我猜你的代码看起来像:

条件跳转或移动取决于未初始化的值:

bool value /* nothing */;
if (value) {
}

无效免费:

char* buf = new char[10000];
buf = &x;
delete [] buf;

现在你发布了代码,我假设第 90 行是这个:

if (!(&(this->theGrid))) { this->clearGrid(); } //If non-empty, destroys grid

问题是你没有初始化theGrid以在构造函数中nullptr,然后你最终可能会delete[]一个非new[] -ed 数组。此外,如果您只是这样做,该代码将读起来更干净:

if (!theGrid) {
    clearGrid();
}

您不需要到处this->。我不明白你为什么要拿theGrid的地址.