I ran into the issue that RtlCaptureContext crashes in release build.The crash is due to the access violation as following,
(1670.944): Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
eax=00000000 ebx=0013f264 ecx=0013f264 edx=00000040 esi=0013f6d4 edi=7c432881
eip=7c9033bc esp=0013f24c ebp=0000000f iopl=0 nv up ei pl nz ac po nc
cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00010212
ntdll!RtlpCaptureContext+0x72:
7c9033bc 8b4504 mov eax,dword ptr [ebp+4] ss:0023:00000013=????????
Interestingly, ebp does not seem to contain a valid address.
Then I unassembled the RtlCaptureContext as the following,
0:000> uf ntdll!RtlpCaptureContext
ntdll!RtlpCaptureContext:
7c90334a 53 push ebx
7c90334b 8b5c2408 mov ebx,dword ptr [esp+8]
7c90334f c783b000000000000000 mov dword ptr [ebx+0B0h],0
7c903359 c783ac00000000000000 mov dword ptr [ebx+0ACh],0
7c903363 c783a800000000000000 mov dword ptr [ebx+0A8h],0
7c90336d c783a400000000000000 mov dword ptr [ebx+0A4h],0
7c903377 c783a000000000000000 mov dword ptr [ebx+0A0h],0
7c903381 c7839c00000000000000 mov dword ptr [ebx+9Ch],0
7c90338b 668c8bbc000000 mov word ptr [ebx+0BCh],cs
7c903392 668c9b98000000 mov word ptr [ebx+98h],ds
7c903399 668c8394000000 mov word ptr [ebx+94h],es
7c9033a0 668ca390000000 mov word ptr [ebx+90h],fs
7c9033a7 668cab8c000000 mov word ptr [ebx+8Ch],gs
7c9033ae 668c93c8000000 mov word ptr [ebx+0C8h],ss
7c9033b5 9c pushfd
7c9033b6 8f83c0000000 pop dword ptr [ebx+0C0h]
7c9033bc 8b4504 mov eax,dword ptr [ebp+4] <<<<<<< access violation here
7c9033bf 8983b8000000 mov dword ptr [ebx+0B8h],eax
7c9033c5 8b4500 mov eax,dword ptr [ebp]
7c9033c8 8983b4000000 mov dword ptr [ebx+0B4h],eax
7c9033ce 8d4508 lea eax,[ebp+8]
7c9033d1 8983c4000000 mov dword ptr [ebx+0C4h],eax
7c9033d7 5b pop ebx
7c9033d8 c20400 ret 4
RtlCaptureContext does not set up ebp as the frame pointer at its prologue, so it uses ebp from the caller. It also means somehow the caller does not handle the ebp properly. Then I found the optimization option for the release build is Maximize Speed (/O2) in VC IDE. Since Maximize Speed (/O2) implicitly enables the option -- Omit Frame Pointers, the instructions to set up ebp as the frame pointer at the function prologue have been removed from the build. Ebp could contain any random value. So to use RtlCatpureContext in the release build, do not enable the option -- Omit Frame Pointers. Otherwise, the application will crash randomly in the release build.
In my case, one approach to resolve the crash is to add /Oy- to explicitly disable the option -- Omit Frame Pointers.
A Bug in a Bag (Collections, Ex-hi-bit 1)
5 hours ago