EXC_BAD_ACCESS (code=1, address=0x0) when calling "glClear(GL_COLOR_BUFFER_BIT);" in hello world program of glfw

0

I'm trying to install OpenGL in CLion on MacOS. My program compiles but when I run it I get this crashreport:

Time Awake Since Boot: 6200 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Segmentation fault: 11
Termination Reason:    Namespace SIGNAL, Code 0xb
Terminating Process:   exc handler [1694]

VM Regions Near 0:
--> 
    __TEXT                 0000000109550000-000000010957c000 [  176K] r-x/r-x SM=COW  /Users/USER/Documents/*

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   ???                             000000000000000000 0 + 0
1   SandBoxGL                       0x000000010955223a main + 362 (main.cpp:30)
2   libdyld.dylib                   0x00007fff6765acc9 start + 1

Thread 1:
0   libsystem_pthread.dylib         0x00007fff6785ab68 start_wqthread + 0

Thread 2:
0   libsystem_pthread.dylib         0x00007fff6785ab68 start_wqthread + 0

Thread 3:
0   libsystem_pthread.dylib         0x00007fff6785ab68 start_wqthread + 0

Thread 4:
0   libsystem_pthread.dylib         0x00007fff6785ab68 start_wqthread + 0

Thread 5:
0   libsystem_pthread.dylib         0x00007fff6785ab68 start_wqthread + 0

Thread 6:
0   libsystem_pthread.dylib         0x00007fff6785ab68 start_wqthread + 0

Thread 7:
0   libsystem_pthread.dylib         0x00007fff6785ab68 start_wqthread + 0

Thread 8:
0   libsystem_pthread.dylib         0x00007fff6785ab68 start_wqthread + 0

Thread 9:: com.apple.NSEventThread
0   libsystem_kernel.dylib          0x00007fff6779be36 semaphore_wait_trap + 10
1   libdispatch.dylib               0x00007fff67601aed _dispatch_sema4_wait + 16
2   libdispatch.dylib               0x00007fff67601fbf _dispatch_semaphore_wait_slow + 98
3   com.apple.HIToolbox             0x00007fff2c35b940 _BeginEventReceiptOnThread + 159
4   com.apple.AppKit                0x00007fff2ab32555 _NSEventThread + 37
5   libsystem_pthread.dylib         0x00007fff6785f109 _pthread_start + 148
6   libsystem_pthread.dylib         0x00007fff6785ab8b thread_start + 15

Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x000000010957d350  rbx: 0x0000000000000000  rcx: 0x00007fbe2b8090ff  rdx: 0x0000000000000002
  rdi: 0x00007fbe2ae0eb40  rsi: 0x00007fbe2af78610  rbp: 0x00007ffee66af8f0  rsp: 0x00007ffee66af898
   r8: 0x00007fff8e186e30   r9: 0x0000000000000010  r10: 0x00007ffee66afa38  r11: 0x0000000109600b46
  r12: 0x0000000000000000  r13: 0x0000000000000000  r14: 0x0000000000000000  r15: 0x0000000000000000
  rip: 0x0000000000000000  rfl: 0x0000000000010202  cr2: 0x0000000000000000

Logical CPU:     4
Error Code:      0x00000014 (no mapping for user instruction read)
Trap Number:     14

...(binary)

It is not the exact same code as on the glfw website, I had to import glad, added the macro on the third line and a couple of prints. The code on the website didn't even compile. That's the code:

#include <iostream>

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

int main(void)
{
    if (!glfwInit()) {
        return -1;
    }

    GLFWwindow* window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);

    std::cout << "window: " << window;
    if (!window)
    {
        std::cout << "problems with window" << std::endl;
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);

    while (!glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT);

        glfwSwapBuffers(window);

        glfwPollEvents();
    }
    glfwTerminate();
    return 0;
}

I installed the glfw library using HomeBrew and the glad library I downloaded on this website https://glad.dav1d.de with gl version 4.1 and all extensions. I added all of the zip folder in the libs/glad folder in my project and linked it in the CMakeLists.txt file. Thats the CMakeLists.txtfile:

cmake_minimum_required(VERSION 3.16)
project(SandBoxGL)

set(SRC_FILES
        src/main.cpp
        )

set(HEADER_FILES)

find_package(glfw3 REQUIRED)
find_package(OpenGl REQUIRED)

add_library(glad "libs/glad/src/glad.c")
include_directories("libs/glad/include")

set(LIBS glfw glad)

add_executable(SandBoxGL ${HEADER_FILES} ${SRC_FILES})

include_directories(
        "${CMAKE_SOURCE_DIR}/src"
        "${CMAKE_SOURCE_DIR}/include"
)


target_link_libraries(${PROJECT_NAME} ${LIBS})

When I run it in the debugger in CLion it stops at the glClear(GL_COLOR_BUFFER_BIT); function and gives me that error message EXC_BAD_ACCESS (code=1, address=0x0). When I remove that line I get a black window as expected. Has anyone an idea how I can fix that?

c++
opengl
glfw
glad
asked on Stack Overflow May 6, 2020 by Gian Laager

1 Answer

1

It is possible that GLAD is not starting up. Try adding something like this:

//Previous stuff
glfwMakeContextCurrent(window);

int gladErr = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); //Starts up GLAD
    assert(gladErr != 0); //Checks glad is ok

    if(GLAD_GL_VERSION_4_1){ //Makes sure OpenGL 4.1 is supported (not really necessary with window hints)
        std::cout << "4.1 OK" << std::endl;
    }

while (!glfwWindowShouldClose(window))
//Render loop
answered on Stack Overflow May 6, 2020 by Misha Melnyk

User contributions licensed under CC BY-SA 3.0