How do I fix this access violation when I exit my custom OpenGL game engine?

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()

__telemetry_main_return_trigger(nullptr);

if(!__scrt_is_managed_app())
exit(main_result)l

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

__scrt_unititialize_crt(true, false);
return main_result;
}

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

if(!__scrt_is_managed_app())
_exit(main_result);


Here is the call stack:

• 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. – Krythic Oct 23 '16 at 3:02
• 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. – JekasG Oct 23 '16 at 4:05