代码错误:线程 1:EXC_BAD_ACCESS(代码=1,地址=0x0)

Posted

技术标签:

【中文标题】代码错误:线程 1:EXC_BAD_ACCESS(代码=1,地址=0x0)【英文标题】:code error : Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) 【发布时间】:2019-04-29 15:58:23 【问题描述】:

我正在尝试使用 OpenGL 用 C++ 编写代码(我使用 GLFW 和 GLEW 库)。代码如下:

#include <GL/glew.h>
#include <GLFW/glfw3.h>

#define numVAOs 1

GLuint renderingProgram;
GLuint vao[numVAOs];

GLuint createShaderProgram()
    const char*vshaderSource =
    "#version 430 \n"
    "void main(void) \n"
    "gl_Position = vec4(0.0,0.0,0.0,1.0);";
    const char*fshaderSource =
    "#version 430 \n"
    "out vec4 color; \n"
    "void main(void) \n"
    "gl_Position = vec4(0.0,0.0,1.0,1.0);";

    GLuint vShader = glCreateShader(GL_VERTEX_SHADER);
    GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER);

    glShaderSource(vShader,1,&vshaderSource,NULL);
    glShaderSource(fShader,1,&fshaderSource,NULL);
    glCompileShader(vShader);
    glCompileShader(fShader);

    GLuint vfProgram = glCreateProgram();
    glAttachShader(vfProgram,vShader);
    glAttachShader(vfProgram,fShader);
    glLinkProgram(vfProgram);
    return vfProgram;





int main()


    glfwInit();

    // Define version and compatibility settings
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
    glGenVertexArrays(numVAOs,vao);
    glBindVertexArray(vao[0]);
    glUseProgram(renderingProgram);
    glDrawArrays(GL_POINT,0,1);
    // Create OpenGL window and context
    GLFWwindow* window = glfwCreateWindow(1430, 800, "Davide", NULL, NULL);
    glfwMakeContextCurrent(window);

    // Check for window creation failure
    if (!window)
    
        // Terminate GLFW
        glfwTerminate();
        return 0;
    


    glewExperimental = GL_TRUE; glewInit();

    // Event loop
    while(!glfwWindowShouldClose(window))
    
        // Clear the screen to black
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT);
        glfwSwapBuffers(window);
        glfwPollEvents();
    

    // Terminate GLFW
    glfwTerminate(); return 0;


不幸的是,当我运行代码时,我得到一个错误代码:

Thread 1: EXC_BAD_ACCESS (code = 1, address = 0x0).

它应该出现一个黑屏,中间有一个点。

【问题讨论】:

【参考方案1】:

多个问题:

    在拥有当前 GL 上下文之前调用 GL 函数:

    glGenVertexArrays(numVAOs,vao);  // nope
    glBindVertexArray(vao[0]);       // nope
    glUseProgram(renderingProgram);  // nope
    glDrawArrays(GL_POINT,0,1);      // nope
    // Create OpenGL window and context
    GLFWwindow* window = glfwCreateWindow(1430, 800, "Davide", NULL, NULL);
    glfwMakeContextCurrent(window);
    

    将这些调用移动到 after glfwMakeContextCurrent()(以及您的 GL 加载程序初始化),以便它们具有可操作的 GL 上下文(并检查 window 是否为 NULL beforeglfwMakeContextCurrent() 中使用它):

    // Create OpenGL window and context
    GLFWwindow* window = glfwCreateWindow(1430, 800, "Davide", NULL, NULL);
    // Check for window creation failure
    if (!window)
    
        // Terminate GLFW
        glfwTerminate();
        return 0;
    
    glfwMakeContextCurrent(window);
    
    glewExperimental = GL_TRUE; glewInit();
    
    glGenVertexArrays(numVAOs,vao);
    glBindVertexArray(vao[0]);
    glUseProgram(renderingProgram);
    glDrawArrays(GL_POINT,0,1);
    

    您还应该在glUseProgram() 之前初始化renderingProgram,也许使用createShaderProgram()

    gl_Position 在片段着色器中无效。您正在考虑 gl_Color 或用户定义的输出,例如您的 color 输出。

    如果您请求的是 GL 3.2 上下文,那么 #version 430 也无效。升级到 GL 4.3 或将着色器降级到 #version 150

    GL_POINT 不是glDrawArrays() 的有效输入。你在想GL_POINTS

    每一帧都调用glDrawArrays(),而不是在你的绘制循环开始时调用一次,这样你就有机会真正看到你的观点。

    GLSL 语句后面需要一个分号。

    无效:

    void main(void)
    
        gl_Position = vec4(0.0,0.0,0.0,1.0)
    ;
    

    有效:

    void main(void)
    
        gl_Position = vec4(0.0,0.0,0.0,1.0);  // note the semicolon
     // note the lack of semicolon
    

大家一起:

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>

void CheckStatus( GLuint obj, bool isShader )

    GLint status = GL_FALSE, log[ 1 << 11 ] =  0 ;
    ( isShader ? glGetShaderiv : glGetProgramiv )( obj, isShader ? GL_COMPILE_STATUS : GL_LINK_STATUS, &status );
    if( status == GL_TRUE ) return;
    ( isShader ? glGetShaderInfoLog : glGetProgramInfoLog )( obj, sizeof( log ), NULL, (GLchar*)log );
    std::cerr << (GLchar*)log << "\n";
    std::exit( EXIT_FAILURE );


void AttachShader( GLuint program, GLenum type, const char* src )

    GLuint shader = glCreateShader( type );
    glShaderSource( shader, 1, &src, NULL );
    glCompileShader( shader );
    CheckStatus( shader, true );
    glAttachShader( program, shader );
    glDeleteShader( shader );


const char* const vert = 1 + R"GLSL(
#version 150
void main()

    gl_Position = vec4(0.0,0.0,0.0,1.0);

)GLSL";

const char* const frag = 1 + R"GLSL(
#version 150
out vec4 color;
void main()

    color = vec4(0.0,0.0,1.0,1.0);

)GLSL";

#define numVAOs 1

GLuint renderingProgram;
GLuint vao[ numVAOs ];

int main()

    glfwInit();

    // Define version and compatibility settings
    glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
    glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 2 );
    glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
    glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE );
    glfwWindowHint( GLFW_RESIZABLE, GL_FALSE );
    // Create OpenGL window and context
    GLFWwindow* window = glfwCreateWindow( 320, 240, "Davide", NULL, NULL );
    // Check for window creation failure
    if( !window )
    
        // Terminate GLFW
        glfwTerminate();
        return 0;
    
    glfwMakeContextCurrent( window );

    glewExperimental = GL_TRUE;
    glewInit();

    glGenVertexArrays( numVAOs, vao );
    glBindVertexArray( vao[ 0 ] );

    GLuint renderingProgram = glCreateProgram();
    AttachShader( renderingProgram, GL_VERTEX_SHADER, vert );
    AttachShader( renderingProgram, GL_FRAGMENT_SHADER, frag );
    glLinkProgram( renderingProgram );
    CheckStatus( renderingProgram, false );
    glUseProgram( renderingProgram );

    // Event loop
    while( !glfwWindowShouldClose( window ) )
    
        // Clear the screen to black
        glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
        glClear( GL_COLOR_BUFFER_BIT );
        glDrawArrays( GL_POINTS, 0, 1 );
        glfwSwapBuffers( window );
        glfwPollEvents();
    

    // Terminate GLFW
    glfwTerminate();
    return 0;

【讨论】:

以上是关于代码错误:线程 1:EXC_BAD_ACCESS(代码=1,地址=0x0)的主要内容,如果未能解决你的问题,请参考以下文章

错误:“线程 1:EXC_BAD_ACCESS(代码=EXC_I386_GPFLT)

得到奇怪的线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x9)swift中的崩溃错误

收到“线程 1:EXC_BAD_ACCESS”错误?

UICollectionView 崩溃并出现错误:线程 1:EXC_BAD_ACCESS

崩溃:“线程 1:EXC_BAD_ACCESS”?

线程1:EXC_BAD_ACCESS(代码=1,地址=0x48)avaudiofoundation