简体   繁体   中英

glShaderSource crashes

I use:

std::string source;
char value;
std::ifstream stream(paths[id]);
while (stream.get(value)) {

    source += value;
}
stream.close()

        
        
int shader = glCreateShader(mode);
shaders[id] = shader;


glShaderSource(shader, 1, (const GLchar* const *)source.c_str(), nullptr);

glCompileShader(shader);

And the app crashes on Also source varible contains exactly:

 #version 330 core
 layout(location = 0) in vec3 aPos;

void main()
{
    gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}

And the app just crashes!

What is the issue?

The problem is here: (const GLchar* const *)source.c_str() . The fact that it didn't work without a cast (effectively a reinterpret_cast ) is a sign that you're doing something wrong.

Save the pointer to a variable: const char *ptr = source.c_str(); , then pass &ptr to glShaderSource .

the problem is in this

glShaderSource(shader, 1, (const GLchar* const *)source.c_str(), nullptr);

try to do this

const char* src = source.c_str();
glShaderSource(shader, 1, &src,nullptr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM