Inverse Heisenbug - Unit test fails only when debugger is attached

后端 未结 4 2089
不知归路
不知归路 2021-01-19 21:39

I recently fixed a defect in our product, the symptom of which was an access violation caused by accessing a dangling pointer.

For good practice I added a unit test

4条回答
  •  天涯浪人
    2021-01-19 21:54

    I have isolated the cause of this problem - see this question for details.

    When running my test harness under the debugger, the memory consumed by the debugging environment meant that subsequent allocations/deallocations of the same object were always allocated in different parts of memory. This meant that when my test harness tried to access a dangling pointer, it crashed the test (technically this is undefined behaviour but this is test code and it seems to do what I need it to do).

    When running my test harness from the command line, subsequent allocations/deallocations of the same object always re-used the same block of memory. This coincedental behaviour meant that when I accessed what was in actuality a dangling pointer in my test case, it happened that the dangling pointer still pointed to a valid object. That's why I didn't see a crash.

提交回复
热议问题