Moosa
Moosa

Reputation: 43

glfwGetFrameBufferSize crashes program with access violation

I've been tryna write a renderer using BGFX, and I've set up a method to get the window width, and height - from my window class that wraps GLFWwindow*;

Here is window.h:

#ifndef WINDOW_H
#define WINDOW_H

#include <types.h>
#include <string>

#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>

class RenderWindow {
public:
    bool Init(u16 _width, u16 _height, std::string _title);
    bool IsCloseRequested() const;

    void* GetNativeWindow();

    u16 GetHeight();
    u16 GetWidth();

    void Refresh() const;
    
    GLFWwindow* GlfwWindowPointer = nullptr;
};

#endif

And here is window.cpp

#include "window.h"

#ifdef _WIN32
#define GLFW_EXPOSE_NATIVE_WIN32
#elif __APPLE__
#define GLFW_EXPOSE_NATIVE_COCOA
#endif
#include <GLFW/glfw3native.h>

#include <iostream>
#include <cstdlib>

bool RenderWindow::Init(u16 _width, u16 _height, std::string _title) {
    glfwInit();
    glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);

    this->GlfwWindowPointer = glfwCreateWindow(_width, _height, _title.c_str(), nullptr, nullptr);
    if(this->GlfwWindowPointer == nullptr) {
        std::cout << "[OS] [Window] [FATAL] Window creation failed, exiting" << std::endl;
        std::exit(10);
    }

    return true;
}

bool RenderWindow::IsCloseRequested() const {
    return glfwWindowShouldClose(this->GlfwWindowPointer);
}

void RenderWindow::Refresh() const {
    glfwPollEvents();
}

u16 RenderWindow::GetHeight() {
    int w = 0, h = 0;
    glfwGetFramebufferSize(&(*GlfwWindowPointer), &w, &h); // ERROR HAPPENS HERE
    return h;
}

u16 RenderWindow::GetWidth() {
    int w = 0, h = 0;
    glfwGetFramebufferSize(&(*GlfwWindowPointer), &w, &h); // ERROR HAPPENS HERE TOO
    return w;
}

void* RenderWindow::GetNativeWindow() {
    #ifdef _WIN32
    return glfwGetWin32Window(this->GlfwWindowPointer);
    #elif __APPLE__
    return glfwGetCocoaWindow(this->GlfwWindowPointer);
    #endif
}

The thing is, whenever I call these methods - the program crashes in vs debugger with the message:

Exception thrown: read access violation.
window was 0xF242340000.

Of course, the address changes every time - but you get the point. Without calling these two functions, my window works effortlessly. I even got bgfx to clear the screen to purple. Please help me.

Upvotes: 0

Views: 103

Answers (0)

Related Questions