简体   繁体   English

版本中的错误不在调试中

[英]Bugs in Release not in Debug

I'm building a top-down view demo for a path planning/collision avoidance method. 我正在为路径规划/避免冲突方法构建一个自顶向下的视图演示。 c++ in Visual Studio 2010. Visual Studio 2010中的c ++。

When I run in Debug mode everything is fine. 当我以Debug模式运行时,一切都很好。 But when I run in Release, crazy behavior occurs. 但是当我在Release中运行时,会发生疯狂的行为。 Previously my characters would speed up immensily and change direction when running into other characters, and now they are just dissapearing (probably moving somewhere outside the screen). 以前,我的角色遇到其他角色时会极大地加快速度并改变方向,而现在却消失了(可能移动到屏幕外的某个地方)。

A search showed that this is usually because of floating point operations. 搜索表明这通常是由于浮点运算引起的。 I removed all floats, and Floating Point Model was already on fp:precise for both Debug and Release. 我删除了所有浮点,并且对于调试和发行版,浮点模型已经在fp:precise上。 I have no idea where to go from here... 我不知道从这里去哪里...

I'm pretty sure this code is the source of the errors. 我很确定这段代码是错误的根源。 Each character can be found in a cell. 每个字符都可以在一个单元格中找到。 On the cell edges, the average velocity of the characters there is stored (cell->vLeft etc), and this character wants to average it's own velocity with that stored on the cell edges. 在像元边缘上,存储了字符的平均速度(像元-> 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);
}   

} }

Any help would be greatly appreciated! 任何帮助将不胜感激!

Edit: And here is _posToCell2D... 编辑:这是_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);
}

One common glitch when switching is the behavior of uninitialized variables. 切换时的一个常见故障是未初始化变量的行为。 Do you ever set all of the uicGrids[][] to some initial value? 您是否曾经将所有uicGrids [] []都设置为某个初始值?

Frequently, Debug will automagically initialize these to 0 for you.. Release leaves them with whatever ram is leftover from previous usage. 通常,Debug会为您自动将它们初始化为0。.Release使它们与以前使用时剩余的内存无关。

如果以上猜测均无济于事,那么调试的一种有效方法是暂时添加许多日志记录语句并将其写入日志文件,以便您可以跟踪代码执行并查看变量的值。

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

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