C++ 调用时的EXC“坏”访问(代码=1,地址=0x0);glClear(GLU颜色缓冲位)&引用;在glfw的hello world计划中
我正在尝试在MacOS上的CLion中安装OpenGL。我的程序可以编译,但运行时会出现以下崩溃报告:C++ 调用时的EXC“坏”访问(代码=1,地址=0x0);glClear(GLU颜色缓冲位)&引用;在glfw的hello world计划中,c++,opengl,glfw,glad,C++,Opengl,Glfw,Glad,我正在尝试在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
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;
}
当我在CLion中的调试器中运行它时,它在
glClear(GL\u COLOR\u BUFFER\u位)处停止代码>函数并向我提供错误消息EXC\u BAD\u ACCESS(代码=1,地址=0x0)
。当我去掉那条线时,我得到了一个黑色的窗口。有人知道我如何解决这个问题吗?可能是因为高兴没有启动。尝试添加如下内容:
//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
//以前的东西
glfwMakeContextCurrent(窗口);
int gladErr=gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)//开始高兴起来
断言(gladErr!=0)//好的
if(GLAD_GL_VERSION_4_1){//确保支持OpenGL 4.1(对于窗口提示来说,这并不是必需的)
标准::cout
//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