为什么Visual Studio Release生成在未执行的代码行中断

Why does Visual Studio Release build break on non-executing code line

本文关键字:执行 代码 中断 Studio Visual Release 为什么      更新时间:2023-10-16

在Visual Studio 10中使用默认发布配置构建c++程序时,我在一条永远不应该到达的线上设置了一个断点。让我感到困惑的是,当运行程序时,它在断点处停止了。

发生中断的代码如下所示(尽管使用此函数创建最小项目不会产生相同的行为)。添加一个volatile语句并将断点移到此行可以使执行不中断。

为什么"释放调试器"(?)会踩在这一行上,并在nto执行时触发断点?如果我在中断后继续执行,代码将继续执行return false语句。

bool BoundingBox::intersects(int32_t xpos, int32_t ypos, int32_t dx, int32_t dy) const
{
int32_t intersectionX;
int32_t intersectionY;
if((dx > 0 && xpos > right)
|| (dx < 0 && xpos < left)
|| (dy > 0 && ypos > top)
|| (dy < 0 && ypos < bot))
return false;
if(dx != 0)
{
intersectionY = ypos + (left - xpos)*dy/dx;
if (intersectionY > bot && intersectionY < top)
return true;
intersectionY = ypos + (right - xpos)*dy/dx;
if (intersectionY > bot && intersectionY < top)
return true;
}
if(dy != 0)
{
intersectionX = xpos + (bot - ypos)*dx/dy;
if (intersectionX > left && intersectionX < right)
return true;
// Could return false here
intersectionX = xpos + (top - ypos)*dx/dy;
if (intersectionX > left && intersectionX < right)
{
// volatile int e = 9;
return true; // !! Code execution will break here
}
}
return false; // Stepping with F10 will move here
}

在发布版本中,这样的行为并不奇怪。有了优化的代码,一行源代码和一个特定地址之间就不再有精确的对应关系。因此断点和单步执行将是不稳定的。