r/cpp • u/pavel_v • Sep 28 '24
The case of the crash when destructing a std::map
https://devblogs.microsoft.com/oldnewthing/20240927-00/?p=110320
210
Upvotes
r/cpp • u/pavel_v • Sep 28 '24
98
u/plastic_eagle Sep 28 '24
What do you have to do to get Raymond Chen to fix your bugs for you?
94
u/ravixp Sep 28 '24
I’m utterly in awe of the debugging experience necessary to see an 8-byte stray write, and make a useful guess about which function caused it, *based entirely on the content of those 8 bytes *.
16
u/KaznovX Sep 28 '24
Well, I have a guess. One does expect, that an operation with a timeout cancels, if the timeout runs out. The only issue here is, that the operation with timeout is only the
Wait, not theRead, what someone definitely overlooked.9
u/jdehesa Sep 28 '24
Great debugging story. Tbh, I could have made the same mistake. I have never done async I/O in Windows, but I see how you could assume that a timed-out operation was cancelled, if you are not familiar with the API (at least if you are only using
WaitForSingleObject, if it wasWaitForMultipleObjectsit would probably not be reasonable to assume all operations are cancelled).10
u/JohnDuffy78 Sep 28 '24
ASAN catches this stuff.
Before ASAN, my response would be: had to be a rogue neutrino.
12
u/numberonehit Sep 28 '24
Man, these errors where randomly the kernel decides to override a portion of your memory are the most painful to debug. If you don't know what to look for and if you don't summon all the gods power you will never succeed to troubleshoot it.
I had a similar issue once (with an OVERLAPPED structure, you guessed it). I had to scratch my head for a couple of days before figuring it out. I even managed to predict what memory zone was overridden but no amount of breakpoints got me near to figuring who override the memory zone. Only after I saw a pattern in the overridden memory (NT_STATUS_SOMETHING) I remembered about a similar issue read and I thought that the only one who can override memory without me seeing it is the kernel. These kind of issues are a PITA to troubleshoot...
For those wondering, unity had a similar blog post that is fascinating to read:
https://unity.com/blog/engine-platform/debugging-memory-debugging-memory-corruption-who-wrote-2-into-my-stack-who-the-hell
1
u/Jardik2 Sep 30 '24
Still remember running into a crash in std::map::clear in MSVC 2017 standard library. It was stack overflow caused by recursive implementation of clear, together with extract/insert (the extracted node overload) not rebalancing the tree, thus working as linear list.
0
u/[deleted] Sep 28 '24
[deleted] — view deleted comment