1. We should define `std::string ret` in where we really need it. According to my test on android platform the function returns at `return "";`, which makes the `ret` unnecessary.

2. `charsWritten` is not used, we should either delete it or use it. Because we don't check the bytes written, I think we can just delete it.
3.  We don't need (logLength+1) space for `logBytes`, because `logBytes`  returned by calling glGetShaderInfoLog(shader, logLength, nullptr, logBytes) takes `logLength` bytes at most, including the ending null character,  i.e. `(strlen(logBytes) + 1 ==  logLength)` always yield true. According to the offical opengles sdk docs: https://www.khronos.org/opengles/sdk/docs/man/xhtml/glGetShaderInfoLog.xml.
and
https://www.khronos.org/opengles/sdk/docs/man/xhtml/glGetShaderiv.xml

4. We don't need `logBytes[logLength] = '\0';`, because `logBytes` returned is already null-terminated.
This commit is contained in:
elloop 2016-01-25 12:22:30 +08:00
parent b85ccbbb4e
commit 341575aa4c
1 changed files with 8 additions and 12 deletions

View File

@ -571,17 +571,15 @@ void GLProgram::use()
static std::string logForOpenGLShader(GLuint shader)
{
std::string ret;
GLint logLength = 0, charsWritten = 0;
GLint logLength = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength);
if (logLength < 1)
return "";
char *logBytes = (char*)malloc(logLength + 1);
glGetShaderInfoLog(shader, logLength, &charsWritten, logBytes);
logBytes[logLength] = '\0';
ret = logBytes;
char *logBytes = (char*)malloc(sizeof(char) * logLength);
glGetShaderInfoLog(shader, logLength, nullptr, logBytes);
std::string ret(logBytes);
free(logBytes);
return ret;
@ -589,17 +587,15 @@ static std::string logForOpenGLShader(GLuint shader)
static std::string logForOpenGLProgram(GLuint program)
{
std::string ret;
GLint logLength = 0, charsWritten = 0;
GLint logLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);
if (logLength < 1)
return "";
char *logBytes = (char*)malloc(logLength + 1);
glGetProgramInfoLog(program, logLength, &charsWritten, logBytes);
logBytes[logLength] = '\0';
ret = logBytes;
char *logBytes = (char*)malloc(sizeof(char) * logLength);
glGetProgramInfoLog(program, logLength, nullptr, logBytes);
std::string ret(logBytes);
free(logBytes);
return ret;