EXC_BAD_ACCESS (code=1, address=0x0) 在glfw的hello world程序中调用"glClear(GL_COLOR_BUFFER_BIT);"时

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

我正在尝试在 MacOS 上的 CLion 中安装 OpenGL。我的程序可以编译,但是当我 运行 它时,我得到了这个崩溃报告:

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)

它与 glfw 网站上的代码不完全相同,我不得不导入 glad,在第三行添加了宏并打印了几张。网站上的代码甚至没有编译。那是代码:

#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;
}

我使用 HomeBrew 安装了 glfw 库,并在本网站 https://glad.dav1d.de 下载了带有 gl 4.1 版和所有扩展的 glad 库。我在项目的 libs/glad 文件夹中添加了所有 zip 文件夹,并将其链接到 CMakeLists.txt 文件中。那是 CMakeLists.txt 文件:

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

当我在 CLion 的调试器中 运行 它时,它在 glClear(GL_COLOR_BUFFER_BIT); 函数处停止并给我错误消息 EXC_BAD_ACCESS (code=1, address=0x0)。当我删除该行时,我得到了预期的黑色 window 。有人知道我该如何解决吗?

GLAD 可能没有启动。尝试添加如下内容:

//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