OpenGL调用Swift EXC_Bad_Access

时间:2015-02-15 18:55:56

标签: ios swift opengl-es

当我编译并运行应用程序时,我收到此错误。通常我必须在Xcode中停止并再次运行它。有时会出现,有时则不然。真烦人

这是我的代码:

func createTextures() {
        let width = 2048;
        let height = 2048;

        texBuffer = GLubyte();

        for (var i = 0; i < foregroundTex.count; i++) {

            if (textures[i + 1] != "null") {
                glGenTextures(1, &foregroundTex[i]);

                glBindTexture(GLenum(GL_TEXTURE_2D), foregroundTex[i]);

                glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_WRAP_S), GL_CLAMP_TO_EDGE);
                glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_WRAP_T), GL_CLAMP_TO_EDGE);

                glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MIN_FILTER), GL_LINEAR);
                glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MAG_FILTER), GL_LINEAR);

                glTexImage2D(GLenum(GL_TEXTURE_2D), GLint(0), GLint(GL_RGBA), GLsizei(width), GLsizei(height), GLint(0), GLenum(GL_RGBA), GLenum(GL_UNSIGNED_BYTE), &texBuffer);

我在最后一次OpenGL调用glTexImage2D()上获得 EXC_BAD_ACCESS

我认为它来自texBuffer变量。也许我没有正确地初始化它?我不知道如何在Swift中初始化它来保存纹理缓冲区。

1 个答案:

答案 0 :(得分:0)

texbuffer需要保存多个字节。 mallocwidth * height * 4

var texBuffer = malloc(width*height*4)