I'm writing an game engine, where the engine is written in a project and exports a .dll file. In another project, in the same solution as the engine, there is a sandbox project which uses the engine. However, there is a bug.

I run the sandbox project in debug mode with the engine dll. When I spam my mouse and keyboard for a few seconds, and close the program via the exit button, the program crashes with an error:

Exception thrown at 0x00000043 in phantom-sandbox.exe: 0xC0000005: Access violation executing location 0x00000043.

If there is a handler for this exception, the program may be safely continued.

I found the source of the bug. Since I'm currently writing the engine for OpenGL, I have to initialize GLEW, and I need to create a HGLRC. If I don't initialize this HGLRC, everything works.

This is not the ideal solution, since I need to use OpenGL for my engine. I went forth without exporting the .dll from the engine, making the engine an application, instead. I made a main.cpp, and wrote it to use the engine, enabling OpenGL rendering. I tried to recreate the bug, but everything works!

I thought it might OpenGL, but then now I'm thinking it might be my engine. How do I fix this?

Here is my code, where it errors:

    int const main_result = invoke_main()



    if(!has_actor) // < this is the line being sent to the call stack

    __scrt_unititialize_crt(true, false);
    return main_result;

__except (_seh_filter_exe(GetExceptionCode(), GetExceptionInformation()))
    int const main_result = GetExceptionCode();


Here is the call stack:

enter image description here

  • \$\begingroup\$ How are you closing the opengl context? Are you terminating the rendering thread before you close it? It seems to me that something is trying to reference the opengl context while it's closing, but the reference is likely null. \$\endgroup\$
    – Krythic
    Oct 23 '16 at 3:02
  • \$\begingroup\$ If you see my comment below you can see the new issue that i have. I call wglDeleteContext in the destructor. But now there is an issue where the command prompt would cause the same bug with its close button. And no i'm not exactly sure how you're supposed to terminate the rendering thread. Please, teach me. \$\endgroup\$
    – JekasG
    Oct 23 '16 at 4:05

What's happening is that the app is dereferencing a pointer that points to protected memory. I don't know a lot about Windows, but it looks to me like your application (or the support code that runs your application) is trying to clean up after running, and is hitting this problem. There are 2 possible causes that I can think of off the top of my head:

  1. You've deallocated an object that the runtime is still using, and now that it's gone when the runtime attempts to use it, the crash occurs.
  2. Something in your app has overwritten some memory that is used by the runtime, and what was once a valid pointer has been overwritten with a garbage address (0x0043).

There are other possibilities, but those seem like the most likely. Usually there are tools for finding memory overwrites. On the Mac we have guard_malloc which will add guard pages around every allocation, and when something is deallocated, it checks to make sure the correct values are contained in the guard region. If not, then it knows that someone overwrote that memory. Perhaps there's something like that on Windows?

  • \$\begingroup\$ I found out that the bug was from wglCreateContext(); I solved the bug by simply calling the wglDeleteContext(); in the class destructor. But then came another issue. Since the program runs with a cmd, when i try to exit the program via pressing the close from the cmd the bug re-appears. So i really want to know how to completely demolish this bug. Also! Why does this only happen when i export the engine as a dll and run it from the sandbox project. Everything works fine when i have the engine as an application and i run the program from there. Q Q \$\endgroup\$
    – JekasG
    Oct 23 '16 at 2:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.