Switch statements and multiple deletions
Errors such as:
Unhandled exception at 0x0098c580 in MenschÄrgereDichNicht.exe: 0xC0000005: Access violation reading location 0xfeeefef6.
or
_vfptr 0xfeeefeee
CXX0030: Error: expression cannot be evaluated
might signal a problem similar to one I encountered. By studying the memory addresses and looking at the call stack, I (correctly, as it turned out) assumed that I was deleting memory used by DirectInput twice. But the thing is, I only had one Release in the entire program for that pointer. It turned out that the problem lay in my switch statement which went through the game states. I thought that calling PostMessage(hwnd, WM_DESTROY, 0, 0); would exit the function and not come back. However, as it turns out, it does come back and I foolishly thought I could leave out the break statements. Inserting those for each of the cases did the trick because the program was no longer able to cascade down the cases and end up posting the WM_DESTROY message multiple times (since it was used for multiple cases), thus deleting the DInput pointer multiple times. Generally, I always put in breaks for clarity and correctness and having now had to track down this annoying error will ensure that I will do so a lot more in the future.
Another associated bug occurred because I didn’t check for the null condition on a pointer to the keyboard device and because of the way my code is structured, ending the program released the keyboard and then zoomed through the Poll_Keyboard function once more. This, of course, was impossible since it had already been released. A simple null check did the trick (although in the future it will probably be better to rewrite the code to avoid that problem altogether).