Crash on glShaderSource?

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!

You’re allocating the char buffer with new and freeing it with free(), which is never good. Try delete[] vs instead.

Thanks so much!
I tried to use delete []vs instead of free(vs), but still has crash. I have no idea.

I’d bet that file textFileRead() returns NULL because the file “shadowmap.vert” is not found. (not in current working directory)

Great!!!
Exactly correct!
Thanks very much!
I’m so careless.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.