glShaderSource() doesn't copy the code correctly

Hi,

my Problem is, that glShaderSource() cuts the Vertex Shader code, so glCompileShader() will fail and report the following error:


0(11) : error C0000: syntax error, unexpected $undefined, expecting "::" at token "<undefined>"

the Vertex Shader code is:


#version 400

layout (location = 0) in vec3 VertexPosition;
layout (location = 1) in vec3 VertexTexCoord;

out vec3 TexCoord;

void main()
{ 
    TexCoord = VertexTexCoord; 
    gl_Position = vec4(VertexPosition, 1.0);
}

glGetShaderSource() returns:


#version 400

layout (location = 0) in vec3 VertexPosition;
layout (location = 1) in vec3 VertexTexCoord;

out vec3 TexCoord;

void main()
{ 
    TexCoord = VertexTexCoord; 
    gl_Position = vec4(VertexPosit�

so glShaderSource() seems to screw up with copying the String. I use the following function to load the shader:


 const char* loadShaderAsString(const char* file){
    std::ifstream shader_file(file, std::ifstream::in);
    std::string str((std::istreambuf_iterator<char>(shader_file)), std::istreambuf_iterator<char>());

    return str.c_str();
}

printf() prints the String correctly.

I’ve tested this with an NVIDIA GTS 450. Operating system is Ubuntu 12.04.

I attached the Eclipse(Indigo) CDT Project.

Hopefully this is the correct forum since this might be a bug.

Thanks for any help,
Apoptose

How do you upload the shader?

glShaderSource itself won’t cut a source string, unless you have a very very odd driver bug (and if so it would break with every other program that uses shaders too) - the most likely cause of this problem is in your own code.

Have a look at the documentation for glShaderSource - you’ll see that the last parameter is an array of string lengths. One probable cause of this is that you’re just passing in the wrong lengths. Other causes are that your file loader is failing to get the full text or is injecting (or somehow otherwise picking up) garbage into the source string that somehow includes a ‘\0’ (unlikely as printf works). Also, won’t str be out of scope after loadShaderAsString returns? You’re probably corrupting the stack here too, so there’s something else to check.

Set a breakpoint and run in your debugger - you’ll be able to check the string that is being passed to glShaderSource, the lengths you’re passing, and backtrack up the call stack until you discover the point at which it goes bad.

Which I’ll bet is not in glShaderSource…

Actually, this will be the problem. Once loadShaderAsString returns the str string object is released and you are returning an invalid pointer. Yes, if you print out immediately the pointer as a string, it will work as nobody else had the chance to overwrite the data at that memory location, but that memory no longer belongs to the string object, it is unallocated memory.

Thanks everyone, that solved the Problem.

Didn’t thought he would free the memory.

After copying the c-String to an new Memory-Location it worked.

Thanks again,
Apoptose