PDA

View Full Version : Crash on glShaderSource?



Liufu
09-27-2010, 09:52 PM
Hello, everybody
I met a weird problem. I initialized the shader, and the code works well in the VS.2005 release mode. But, it will cash when I run it as a .exe file.

I initialized it like this:
_initShader()
{
char *vs = NULL;
const char *use_vs = NULL;
program = glCreateProgram();
vs = textFileRead("shadowmap.vert");
use_vs = vs;

GLuint vShader = glCreateShader(GL_VERTEX_SHADER);//If I remove this line, the code will work well without crash.
glShaderSource(vShader, 1, &use_vs, NULL);
free(vs);
glCompileShader(vShader);
glAttachShader(program, vShader);
glLinkProgram(program);
}

char* textFileRead(const GLchar *filename)
{
FILE *fp;
char *content = NULL;

int count=0;

if (filename != NULL) {
fp = fopen(filename,"rt");

if (fp != NULL) {

fseek(fp, 0, SEEK_END);
count = ftell(fp);
rewind(fp);

if (count > 0) {
content = new char[sizeof(char) * (count+1)];
count = fread(content,sizeof(char),count,fp);
content[count] = '\0';
}
fclose(fp);
}
}
return content;
}

#version 120
#extension GL_EXT_gpu_shader4 : enable
uniform sampler2D posTex;
uniform sampler2D quatTex;
const float PI = 3.1415926;
void main(void)
{
.............................
rotatedPos.xyz = rotatedPos.xyz + centerOfMass.xyz;
gl_Position = gl_ModelViewProjectionMatrix * rotatedPos;
gl_FrontColor = gl_Color;
}
Thanks very much!

malexander
09-27-2010, 10:56 PM
You're allocating the char buffer with new and freeing it with free(), which is never good. Try delete[] vs instead.

Liufu
09-27-2010, 11:22 PM
Thanks so much!
I tried to use delete []vs instead of free(vs), but still has crash. I have no idea.

mfort
09-28-2010, 02:46 AM
I'd bet that file textFileRead() returns NULL because the file "shadowmap.vert" is not found. (not in current working directory)

Liufu
09-28-2010, 04:14 AM
Great!!!
Exactly correct!
Thanks very much!
I'm so careless.