Lau*_*ent 5 c++ debugging c++-cli visual-studio visual-studio-debugging
我在调试 Visual Studio 项目时遇到问题。该项目有一个大型的中心类“MachineControl”。它还将当前所处的状态存储为枚举类的值。在尝试修复不相关的错误时,我设置了一些条件断点来解决它。经过一些令人困惑的情况后,我意识到从调试器获得的信息是错误的。代码运行时的值与调试器显示的值不同。
这个值是错误的,因为当我单步执行代码时,它实际上表现得好像处于 StateEnum::Running(1) 状态。
在与调试器相关的所有内容中发现错误信息:
正确信息:
环境:
代码:
错误的调试信息发生在难以隔离的点。可悲的是,我无法生成一个最小的可重复示例。以下代码片段是我必须显示问题发生的一般情况的最简单的说明性代码:
class MachineControl : public EventReceiver <Evt>
{
StateEnum mState;
public:
void onEventReceived(const Evt& evt) override;
void processPeriodicTimer();
//...
};
void MachineControl::onEventReceived(const Evt& event) // called by other class through managed to native transition
{
switch (mState) // shows wrong information
{
case StateEnum::Running:
processRunning(event); // <-- code jumps here, even though mState is not shown to be in StateEnum::Running
break;
// handle other cases
}
//...
}
void MachineControl::processPeriodicTimer() // called from inside
{
switch (mState) // shows correct information
{
// ...
}
//...
}
// a thousand more lines of code before and after this
Run Code Online (Sandbox Code Playgroud)
上图展示了显示错误调试信息时的调用堆栈。
void MachineControl::onEventReceived(const Evt& event)
{
usessIndirection(event);
}
void MachineControl::uselessIndirection(const Evt& event)
{
switch (mState)
{
//...
}
//...
}
Run Code Online (Sandbox Code Playgroud)
这两个“解决方案”并不令人满意,因为我不想到处添加局部变量和不必要的函数调用,只是为了使调试按预期工作。似乎有一个根本问题需要引起注意。
这个问题确实扰乱了我的调试工作,我很高兴听到任何建议或后续问题。谢谢!
归档时间: |
|
查看次数: |
454 次 |
最近记录: |