I am encountering an odd crash in our software and I’m having a lot of trouble debugging it, and so I am seeking SO’s advice on how to tackle it.
The crash is an access violation reading a NULL pointer:
First chance exception at $00CF0041.
Exception class $C0000005 with message
‘access violation at 0x00cf0041: read
of address 0x00000000’.
It only happens ‘sometimes’ – I haven’t managed to figure out any rhyme or reason, yet, for when – and only in the main thread. When it occurs, the call stack contains one incorrect entry:

For the main thread, which this is, it should show a large stack full of other items.
At this point, all other threads are inactive (mostly sitting in WaitForSingleObject or a similar function.) I have only seen this crash occur in the main thread. It always has the same call stack of one entry, in the same method at the same address. This method may or may not be related – we do use the VCL in our application. My bet, though, is that something (possibly quite a while ago) is corrupting the stack, and the address where it’s crashing is effectively random. Note it has been the same address across several builds, though – it’s probably not truly random.
Here is what I’ve tried:
- Trying to reproduce it reliably at a certain point. I have found nothing that reproduces it every time, and a couple of things that occasionally do, or do not, for no apparent reason. These are not ‘narrow’ enough actions to narrow it down to a particular section of code. It may be timing related, but at the point the IDE breaks in, other threads are usually doing nothing. I can’t rule out a threading problem, but think it’s unlikely.
- Building with extra debugging statements (extra debug info, extra asserts, etc.) After doing so, the crash never occurs.
- Building with Codeguard enabled. After doing so, the crash never occurs and Codeguard shows no errors.
My questions:
1. How do I find what code caused the crash? How do I do the equivalent of walking back up the stack?
2. What general advice do you have for how to trace the cause of this crash?
I am using Embarcadero RAD Studio 2010 (the project mostly contains C++ Builder code and small amounts of Delphi.)
Edit: I thought I should add what actually caused this. There was a thread that called ReadDirectoryChangesW and then, using GetOverlappedResult, waited on an event to continue and do something with the changes. The event was also signalled in order to terminate the thread after setting a status flag. The problem was that when the thread exited it never called CancelIO. As a result, Windows was still tracking changes and probably still writing to the buffer when the directory changed, even though the buffer, overlapped structure and event no longer existed (nor did the thread context in which they were created.) When CancelIO was called, there were no more crashes.
Even when the IDE-provided stack trace isn’t very complete, that doesn’t mean there isn’t still useful information on the stack. Open up the CPU view and check out the stack pane; for every CALL opcode, a return address is pushed on the stack. Since the stack grows downwards, you’ll find these return addresses above the current stack location, i.e. by scrolling upwards in the stack pane.
The stack for the main thread will be somewhere around $00120000 or $00180000 (address space randomization in Vista and upwards has made it more random). Code for the main executable will be somewhere around $00400000. You can speculatively investigate elements on the stack that don’t look like integer data (low values) or stack addresses ($00120000+ range) by right-clicking on the stack entry and selecting Follow -> Near Code, which will cause the disassembly window to jump to that code address. If it looks like invalid code, it’s probably not a valid entry in the stack trace. If it’s valid code, it may be OS code (frequently around $77000000 and above) in which case you won’t have meaningful symbols, but every so often you’ll hit on an actual proper stack entry.
This technique, though somewhat laborious, can get you meaningful stack trace info when the debugger isn’t able to trace things through. It doesn’t help you if ESP (the stack pointer) has been screwed with, though. Fortunately, that’s pretty rare.