Frame not in module problem in VS2019

Hello I I ran into a bug called Frame Not in Module under Release when I was using VS2019. It’s OK when I was trying to build it under Debug.
I’m using GLFW3 and glad with derect_state_access and debug_output. Actually I dont think this is the problem. I fought with this bug line by line and I’m sure I found it. Howerver I cannot solve it.
It’s OK when I worte these lines in main function:

glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//constexpr unsigned int width = 1000;
//constexpr unsigned int height = 1000;
GLFWwindow *window = glfwCreateWindow(width, height, "a random name", nullptr, nullptr);
assert(m_window != nullptr);
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
asset(gladLoadGLLoader((GLADloadproc)glfwGetProcAddress));
#ifdef _DEBUG
glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
glDebugMessageCallback(debug_message_callback, nullptr);
#endif
glEnable(GL_DEPTH_TEST);

These code are just initializing a window that would be used in my project. It’s OK.
But when I was trying to encapsulate these code, strange things happened.

void Window::init(){
	glfwInit();

	//OpenGL 4.5
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

	m_window = glfwCreateWindow(m_screen_width, m_screen_height, "myname", nullptr, nullptr);

	//m_window not nullptr
	assert(m_window != nullptr);
	glfwMakeContextCurrent(m_window);
	/*
	* glfwSetWindowUserPointer
	* glfwGetWindowUserPointer
	* set -> get
	* used in callback functions
	*/
	glfwSetWindowUserPointer(m_window, this);

	//callback functions

	//framebuffer size callback
	auto framebuffer_size_callback = [](GLFWwindow* window, int width, int height) {
		glViewport(0, 0, width, height);
	};
	glfwSetFramebufferSizeCallback(m_window, framebuffer_size_callback);
	glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

	//check glad
	assert(gladLoadGLLoader((GLADloadproc)glfwGetProcAddress));
        ....
}

and this will be called in constructor of class Window.

Window::Window()
{
     ...
     init();
     ...
}
    auto window = std::make_unique<Window>(...);

These code are just not in main function and I checked that I didn’t miss anything. Then whatever I do I ran into a bug called Frame Not in Module under Release. I tried like some simple openGL api like glGenFramebuffers, there will always be:

Exception thrown at 0x0000000000000000 in test.exe: 0xC0000005: Access violation executing location 0x0000000000000000.

This is strange because I found guys in github wrote similar code and it’s just OK. I build my whole project under Debug and it works well.

Oh I figure it out.
Here:

 assert(gladLoadGLLoader((GLADloadproc)glfwGetProcAddress));

This macro only works under DEBUG as we all know.
I checked gladLoadGLLoader this function in glad.c:

int gladLoadGLLoader(GLADloadproc load) {
	GLVersion.major = 0; GLVersion.minor = 0;
	glGetString = (PFNGLGETSTRINGPROC)load("glGetString");
	if(glGetString == NULL) return 0;
	if(glGetString(GL_VERSION) == NULL) return 0;
	find_coreGL();
	load_GL_VERSION_1_0(load);
	load_GL_VERSION_1_1(load);
	load_GL_VERSION_1_2(load);
	load_GL_VERSION_1_3(load);
	load_GL_VERSION_1_4(load);
	load_GL_VERSION_1_5(load);
	load_GL_VERSION_2_0(load);
	load_GL_VERSION_2_1(load);
	load_GL_VERSION_3_0(load);
	load_GL_VERSION_3_1(load);
	load_GL_VERSION_3_2(load);
	load_GL_VERSION_3_3(load);
	load_GL_VERSION_4_0(load);
	load_GL_VERSION_4_1(load);
	load_GL_VERSION_4_2(load);
	load_GL_VERSION_4_3(load);
	load_GL_VERSION_4_4(load);
	load_GL_VERSION_4_5(load);

	if (!find_extensionsGL()) return 0;
	load_GL_EXT_direct_state_access(load);
	load_GL_KHR_debug(load);
	return GLVersion.major != 0 || GLVersion.minor != 0;
}

So this function will do a lot of things during initialization. I thought it was a function used to check if glad is valid or properly used. Now I know I’m totally wrong. I changed my code like:

	//check glad
	auto loader = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
	assert(loader != 0);

Everything is OK now.:grinning: