bug在发布中,不在调试中

Bugs in Release not in Debug

本文关键字:调试 布中 bug      更新时间:2023-10-16

我正在为路径规划/避免碰撞方法构建一个自上而下的视图演示。c++ in Visual Studio 2010.

当我运行在调试模式一切都很好。但当我在释放中运行时,疯狂的行为发生了。以前我的角色在遇到其他角色时会加速并改变方向,而现在他们只是消失了(可能移动到屏幕外的某个地方)。

搜索表明,这通常是由于浮点操作。我删除了所有的浮点数,并且浮点模型已经在调试和发布时都是精确的。我不知道从这里该去哪里…

我很确定这段代码是错误的来源。每个字符都可以在一个单元格中找到。在单元边缘,字符的平均速度被存储(cell->vLeft等),并且这个字符想要平均它自己的速度与存储在单元边缘上的速度。

void CharacterQueryStructure_Grid::computeActualVelocity(Character& character, double dt){
// find the cell for this character
const CellCoord& cellID = _posToCell2D(character.getPosition());
int x = cellID.first, y = cellID.second;
int layerID = character.getLayer();
if(x == -1 && y == -1){ // if the position is invalid, return no neighbours
    //TODO: ERRORRRR
    //return Point(NULL, NULL);
    character.setNewVelocity_EulerIntegration(character.getPreferredVelocity(), dt);
}
else{
    //Interpolate between the four points to find the average velocity at that location
    UIC_cell* cell = uicGrids[layerID][_getCellID(x,y)];
    double distLeft = (character.getPosition().x - xMin) - cellSize * x;
    double distBot  = (character.getPosition().y - yMin) - cellSize * y;
    //First find the interpolated velocity at the location of your projection on the horizontal and vertical axes.
    Point vHor = cell->vLeft * ((cellSize - distLeft)/cellSize) + cell->vRight * (distLeft/cellSize);
    Point vVer = cell->vBot  * ((cellSize - distBot)/cellSize)  + cell->vTop * (distBot/cellSize);
    //Now interpolate these to your location
    double distHor = abs(distLeft - .5 * cellSize);
    double distVer = abs(distBot - .5 * cellSize);
    //Point vAverage = vHor * (distHor / (.5 * cellSize)) + vVer * (distVer / (.5 * cellSize));
    Point vAverage = (vHor + vVer) / 2;

    //Calculate the new velocity based on your own preferred velocity, the average velocity and the density in your cell.
    Point newVelo = character.getPreferredVelocity() + (cell->density / maxDensity) * (vAverage - character.getPreferredVelocity());
    // set it, while adhering smoothness constraints
    character.setPreferredVelocity(newVelo);
}   

}

任何帮助都将非常感激!

编辑:这里是_posToCell2D…

CellCoord CharacterQueryStructure_Grid::_posToCell2D(const Point& pos) const
{
    int x = (int)floor((pos.x - xMin) / cellSize);
    int y = (int)floor((pos.y - yMin) / cellSize);
    // if this coordinate lies outside the grid: return nothing
    if(x < 0 || y < 0 || x >= xNrCells || y >= yNrCells)
        return CellCoord(-1,-1);
    // otherwise, return the correct cell ID
    return CellCoord(x,y);
}

切换时的一个常见故障是未初始化变量的行为。您是否曾经将所有的uicGrids[][]设置为一些初始值?

通常,Debug会自动将这些初始化为0。

如果以上猜测都没有帮助,那么调试的有效方法是临时添加大量日志语句并写入日志文件,以便您可以跟踪代码执行并查看变量的值。