I’m trying to use GLFW inside a static library. If I initialize a GLFW window inside the library’s primary function (Application), it works. But if I abstract it to a separate class, I get an access violation error. I’ve linked glfw to both the library and the executable:
#include "glad/glad.h"
#include "glfw3.h"
#include "Window/Window.h"
Application::Application() : m_Camera(1.0f, 1.0f, 1.0f, 1.0f) {
std::unique_ptr<Window> m_Window;
m_Window = std::unique_ptr<Window>();
}
bool Application::init() {
m_Window->init();} //access violation
class Window:
bool Window::init() {
if (!glfwInit()) HZ_CORE_CRITICAL("GLFW FAILED");
//glfwSetErrorCallback(GLFWErrorCallback);
m_gWindow = glfwCreateWindow(800, 600, "ALAvery", NULL, NULL);
if (!m_gWindow) { HZ_CORE_CRITICAL("GLFW WINDOW CREATION FAILED"); return false; }
glfwMakeContextCurrent(m_gWindow);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
int status = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
HZ_CORE_ASSERT(status, "Failed to initialize Glad!");
>Solution :
You are trying everything except for looking for examples of usage.
Application::Application() : m_Camera(1.0f, 1.0f, 1.0f, 1.0f), m_Window{ new Window }
{
;
}
When you have:
Application::Application() : m_Camera(1.0f, 1.0f, 1.0f, 1.0f) {
std::unique_ptr<Window> m_Window; // creating a local variable that shadows class m_Window...
m_Window = std::unique_ptr<Window>(); // copy assigment of empty unique_ptr (std::unique_ptr<Window>() = default construction of unique_ptr)
}
Basically you did same as if you just deleted those lines and did nothing.
Since the c++17 specification you can also use:
m_Window = std::make_unique<Window>();
or:
Application::Application() : m_Camera(1.0f, 1.0f, 1.0f, 1.0f), m_Window{ std::make_unique<Window>() }