I came across a difficult to debug situation in one of my real projects, where I accidentally got a link to a local variable inside a moved lambda. Access was made from another thread, but the moved lambda was maintained until the second thread ended.
The error occurred only when optimization was disabled and was caused by careless refactoring.
I created a minimal example ( available here on wandbox ) that reproduces the problem:
struct state { int x = 100; }; template <typename TF> void eat1(TF&& f) {
Surprisingly, not one of the disinfectants and warning flags could help here.
I have tried the following combinations of compilers and disinfectants:
-Wall -Wextra -Wpedantic -g -O0
flags are always enabled:
Compiled by: g ++ 6.1.1 on Arch Linux x64; clang ++ 3.8.0 on Arch Linux x64; g ++ 5.3.1 on Fedora x64; clang ++ 3.7.0 on Fedora x64.
Disinfectants: -fsanitize=address ; -fsanitize=undefined , -fsanitize=thread .
None of the combinations produced any useful diagnosis. I expected either AddressSanitizer to tell me that I was accessing a sagging link, or UndefinedSanitizer to catch UB when accessing it, or ThreadSanitizer to tell me that a separate thread has access to an invalid memory location.
Is there a reliable way to diagnose this problem? Should I put this example in any of the disinfectant error tracing as a function request / defect?
source share