了解 heisenbug 示例:"Debuggers cause additional source code to be executed stealthily"

Understanding heisenbug example: "Debuggers cause additional source code to be executed stealthily"

我阅读了有关 heisunbug 的维基百科页面,但不理解这个示例。谁能详细解释一下?

Debuggers also commonly provide watches or other user interfaces that cause additional source code (such as property accessors) to be executed stealthily, which can, in turn, change the state of the program.

我认为它的意思是调试器本身可能会调用代码(例如 getters)来检索您可能已放置手表的 属性 的值。

考虑 getter:

def getter fahrenheit:
    return celsius * 9 / 5 + 32;

如果你把手表放在 fahrenheit 属性 上会发生什么。

通常只有当您的代码本身尝试访问 fahrenheit 属性 时才会调用该代码,但是,如果调试器调用它来维护监视,则可能会在控制你的程序。

一个简单的例子,假设 getter 有一个(非常明显的)错误,这意味着它 return 第一次调用时的结果是错误的:

class temperature:
    variable state

    def init:
        state = 1

    def getter fahrenheit:
        if state == 1:
            state = 0
            return -42
        return celsius * 9 / 5 + 32;

所以 运行 你的代码 在没有 调试器的情况下会出现问题,因为它会 return 在你的代码第一次调用它时得到一个奇怪的值。

但是,如果您的 调试器 实际上正在调用 getter 来提取它正在监视的值(并且它可能在 每个 您执行的单步操作),这意味着 getter 将在您的代码调用时正确 return 正确 值认为是第一次。

因此,当您尝试仔细观察问题时,问题就会消失,这就是 Heisenbug 的 定义,尽管事实上 Heisenberg actual测不准原理与观察者效应关系不大