簡體   English   中英

EXC_BAD_ACCESS (code=1, address=0x0) 當調用“glClear(GL_COLOR_BUFFER_BIT);” 在 glfw 的 hello world 程序中

[英]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 網站上的代碼不完全相同,我不得不導入高興,在第三行添加了宏和幾個打印。 網站上的代碼甚至沒有編譯。 那是代碼:

#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 和所有擴展。 我在項目的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); function 並給我錯誤消息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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM