Jump to content
  • Advertisement
Sign in to follow this  
noodleBowl

glGetActiveUniform causes access violation?

This topic is 1356 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm attempting to get the active uniforms of my shaders, but I'm running into a very strange issue.
When ever I execute the code below I automatically crash and get an access violation :(
 

GLint size;
GLenum type;
std::string name;
glGetActiveUniform(programID, 0, GL_ACTIVE_UNIFORM_MAX_LENGTH, NULL, &size, &type, &name[0]);

 
The issues seems to come from GL_ACTIVE_UNIFORM_MAX_LENGTH, because when I run this code
 

GLint size;
GLenum type;
std::string name;
glGetActiveUniform(programID, 0, 10, NULL, &size, &type, &name[0]);

Everything runs perfectly normal and my application does not crash.

 

Any ideas why this happens? Am I doing something wrong and just not seeing it?

Or do they know what the value of GL_ACTIVE_UNIFORM_MAX_LENGTH is?

Share this post


Link to post
Share on other sites
Advertisement
You're telling GL to copy characters into the buffer at address "&name[0]"... This looks incredibly scary (wrong).

There is not a buffer of N characters allocated at that address for you or GL to write into.
You can't just treat a C++ string like that.

The fact that this *seems to work* with N=10 is even more scary, as you've actually just got yourself a buffer overrun / memory corruption bug!

Use a fixed size C string instead:
GlChar name[GL_ACTIVE_UNIFORM_MAX_LENGTH+1];

Share this post


Link to post
Share on other sites

You're telling GL to copy characters into the buffer at address "&name[0]"... This looks incredibly scary (wrong).

 
Its weird cause using glGetActiveAttribute like this had no issue. But either way its wrong
 
 

Use a fixed size C string instead:
GlChar name[GL_ACTIVE_UNIFORM_MAX_LENGTH+1];

 
Is there a way to copy the content that gets placed in there to a std::string?( Is this even a good idea? )
A roughly 40K (supposed value of [background=#fafbfc]GL_ACTIVE_UNIFORM_MAX_LENGTH) [/background]array of GLchars where 99% of it is unused does not site well with me
 
Also on a complete side note:
 
When using
GLchar name[GL_ACTIVE_UNIFORM_MAX_LENGTH+1];
glGetActiveUniform(programID, location, GL_ACTIVE_UNIFORM_MAX_LENGTH, &length, &size, &type, name);
Why can I pass in the variable 'name' without using the & address reference and not have an issue with my compiler? Where with any of the other pass in variables used by the function I get an error, even though they are all values passed in by pointer(eg GLsizei *length). Is that because GLchar[] is somehow a pointer to an address of memory by default?

Also is doing &name[0] bad? Or is that only because I was trying to use std::string? What about the case with std::vector<GLchar>? Edited by noodleBowl

Share this post


Link to post
Share on other sites

You're using GL_ACTIVE_UNIFORM_MAX_LENGTH incorectly. It's not a value you use directly as a length, but as a key to fetch the longest uniform name using glGetProgramiv. The correct usage should be:

GLint maxLength;
glGetProgramiv(program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxLength);
 
std::string uniformName(maxLength); // the value returned from glGetProgramiv includes the null terminator character.

// use glGetActiveUniform to fetch the uniform info, using maxLength as bufSize, passing &uniformName[0] as the 'name' pointer.

Alternatively, you could use a fixed char[] array of size 1024 + 3 + 1 (1024 is the longest identifier name allowed in GLSL, 3 in case you have a uniform array which would cause the name to end with "[0]", and an extra 1 for the null terminator.

 

Why can I pass in the variable 'name' without using the & address reference and not have an issue with my compiler? Where with any of the other pass in variables used by the function I get an error, even though they are all values passed in by pointer(eg GLsizei *length). Is that because GLchar[] is somehow a pointer to an address of memory by default?

Passing 'name' causes the array to decay to a pointer (i.e. it becomes &name[0]). This can happen any time where you're passing an array to an argument expecting a pointer.

Edited by Xycaleth

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!