EXC_BAD_ACCESS on glDrawElements and Vertex Buffer Objects

Hi guys,

I wrote a simple application using GLUT that I’m porting to SDL now to turn it into a game.

I am having a bizarre issue specifically with using glDrawElements and Vertex Buffer Objects, SDL 1.2.14 OSX. If I don’t use VBO’s the program runs fine. It only throws a “EXC_BAD_ACCESS” when using VBO’s. To make the matter more obscure. The program runs totally fine in GLUT. Am I missing something?

Any help would be most appreciated.

Thanks,

Leonard

Here’s the drawing code:

if (glewGetExtension("GL_ARB_vertex_buffer_object"))
{
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);		
	
	//Load vertices
	glBindBuffer(GL_ARRAY_BUFFER, this->mesh->vbo_vertices);
	glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
	
	//Load normals
	glBindBuffer(GL_ARRAY_BUFFER, this->mesh->vbo_normals);
	glNormalPointer(GL_FLOAT, 0, BUFFER_OFFSET(0));

	//Load UVs
	glBindBuffer(GL_ARRAY_BUFFER, this->mesh->vbo_uvs);
	glTexCoordPointer(2, GL_FLOAT, 0, BUFFER_OFFSET(0));

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->mesh->vbo_index);
	App dies here -----> glDrawElements(GL_TRIANGLES, 3*this->mesh->numFaces, GL_UNSIGNED_INT, BUFFER_OFFSET(0));

	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_NORMAL_ARRAY);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);

} else {

	//BTW: If I run this block of code instead of the above, everything renders fine. App doesn't die. 

	//Drawing with vertex arrays
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);

	glVertexPointer(3, GL_FLOAT, 0, this->mesh->vertexArray);
	glNormalPointer(GL_FLOAT, 0, this->mesh->normalsArray);
	glTexCoordPointer(2, GL_FLOAT, 0, this->mesh->uvArray);

	glDrawElements(GL_TRIANGLES, 3*this->mesh->numFaces, GL_UNSIGNED_INT, this->mesh->indexArray);

	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_NORMAL_ARRAY);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

Here’s the debug information:

Program received signal:  ?EXC_BAD_ACCESS?.

Thread-1-<com.apple.main-thread>
#0	0x17747a93 in gleRunVertexSubmitImmediate
#1	0x1774772c in gleLLVMArrayFunc
#2	0x177476e4 in gleSetVertexArrayFunc
#3	0x1773073c in gleDrawArraysOrElements_ExecCore
#4	0x176baa7b in glDrawElements_Exec
#5	0x97524050 in glDrawElements

asm gleRunVertexSubmitImmediate

0x17747a93  <+0771>  mov    (%eax,%ecx,4),%eax		<-- the app dies on this.

Here’s my SDL initialization code:

//Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
	cout << "Could not initialize SDL" << endl << SDL_GetError();
	exit(2);
}

//Set window
SDL_WM_SetCaption("Hello World!", "Hello World!");

//Set openGL window
if ( SDL_SetVideoMode(width, height, 32, SDL_OPENGL | SDL_RESIZABLE) == NULL ) {
	cout << "Unable to create OpenGL context: %s\n" << endl << SDL_GetError();
	SDL_Quit();
	exit(2);
}

//Set up event handling
SDL_Event event;
bool quit = false;

//Initialize GLEW
GLenum err = glewInit();
if (GLEW_OK != err)
{
	//Problem: glewInit failed, something is seriously wrong. 
	fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
	exit(1);
}
fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));

I think you need to load OpenGL extensions through SDL’s interface:
http://wiki.libsdl.org/moin.cgi/SDL_GL_LoadLibrary
http://wiki.libsdl.org/moin.cgi/SDL_GL_GetProcAddress

I hope that helps,
-AlexOn Wed, Nov 3, 2010 at 12:07 PM, Leonard Teo <teo.leonard at gmail.com> wrote:

Hi guys,

I wrote a simple application using GLUT that I’m porting to SDL now to turn
it into a game.

I am having a bizarre issue specifically with using glDrawElements and
Vertex Buffer Objects, SDL 1.2.14 OSX. If I don’t use VBO’s the program runs
fine. It only throws a “EXC_BAD_ACCESS” when using VBO’s. To make the matter
more obscure. The program runs totally fine in GLUT. Am I missing something?

Any help would be most appreciated.

Thanks,

Leonard

Here’s the drawing code:

   if (glewGetExtension("GL_ARB_vertex_buffer_object"))
   {
           glEnableClientState(GL_VERTEX_ARRAY);
           glEnableClientState(GL_NORMAL_ARRAY);
           glEnableClientState(GL_TEXTURE_COORD_ARRAY);

           //Load vertices
           glBindBuffer(GL_ARRAY_BUFFER, this->mesh->vbo_vertices);
           glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));

           //Load normals
           glBindBuffer(GL_ARRAY_BUFFER, this->mesh->vbo_normals);
           glNormalPointer(GL_FLOAT, 0, BUFFER_OFFSET(0));

           //Load UVs
           glBindBuffer(GL_ARRAY_BUFFER, this->mesh->vbo_uvs);
           glTexCoordPointer(2, GL_FLOAT, 0, BUFFER_OFFSET(0));

           glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,

this->mesh->vbo_index);
App dies here -----> glDrawElements(GL_TRIANGLES,
3*this->mesh->numFaces, GL_UNSIGNED_INT, BUFFER_OFFSET(0));

           glDisableClientState(GL_VERTEX_ARRAY);
           glDisableClientState(GL_NORMAL_ARRAY);
           glDisableClientState(GL_TEXTURE_COORD_ARRAY);

   } else {

           //BTW: If I run this block of code instead of the above,

everything renders fine. App doesn’t die.

           //Drawing with vertex arrays
           glEnableClientState(GL_VERTEX_ARRAY);
           glEnableClientState(GL_NORMAL_ARRAY);
           glEnableClientState(GL_TEXTURE_COORD_ARRAY);

           glVertexPointer(3, GL_FLOAT, 0, this->mesh->vertexArray);
           glNormalPointer(GL_FLOAT, 0, this->mesh->normalsArray);
           glTexCoordPointer(2, GL_FLOAT, 0, this->mesh->uvArray);

           glDrawElements(GL_TRIANGLES, 3*this->mesh->numFaces,

GL_UNSIGNED_INT, this->mesh->indexArray);

           glDisableClientState(GL_VERTEX_ARRAY);
           glDisableClientState(GL_NORMAL_ARRAY);
           glDisableClientState(GL_TEXTURE_COORD_ARRAY);
   }

Here’s the debug information:

   Program received signal:  ?EXC_BAD_ACCESS?.

   Thread-1-<com.apple.main-thread>
   #0      0x17747a93 in gleRunVertexSubmitImmediate
   #1      0x1774772c in gleLLVMArrayFunc
   #2      0x177476e4 in gleSetVertexArrayFunc
   #3      0x1773073c in gleDrawArraysOrElements_ExecCore
   #4      0x176baa7b in glDrawElements_Exec
   #5      0x97524050 in glDrawElements

   asm gleRunVertexSubmitImmediate

   0x17747a93  <+0771>  mov    (%eax,%ecx,4),%eax          <-- the app

dies on this.

Here’s my SDL initialization code:

   //Initialize SDL
   if (SDL_Init(SDL_INIT_VIDEO) < 0)
   {
           cout << "Could not initialize SDL" << endl <<

SDL_GetError();
exit(2);
}

   //Set window
   SDL_WM_SetCaption("Hello World!", "Hello World!");

   //Set openGL window
   if ( SDL_SetVideoMode(width, height, 32, SDL_OPENGL | SDL_RESIZABLE)

== NULL ) {
cout << “Unable to create OpenGL context: %s\n” << endl <<
SDL_GetError();
SDL_Quit();
exit(2);
}

   //Set up event handling
   SDL_Event event;
   bool quit = false;

   //Initialize GLEW
   GLenum err = glewInit();
   if (GLEW_OK != err)
   {
           //Problem: glewInit failed, something is seriously wrong.
           fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
           exit(1);
   }
   fprintf(stdout, "Status: Using GLEW %s\n",

glewGetString(GLEW_VERSION));


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Hi,On Wed, Nov 3, 2010 at 10:38 AM, Alex Barry <alex.barry at gmail.com> wrote:

           App dies here -----> glDrawElements(GL_TRIANGLES,

3*this->mesh->numFaces, GL_UNSIGNED_INT, BUFFER_OFFSET(0));

This looks funky to me. I think you mean:
glDrawElements(GL_TRIANGLES, this->mesh->numFaces, GL_UNSIGNED_INT,
BUFFER_OFFSET(0));
Ian

Thanks for checking, but I don’t think that’s the problem as there are 3 index values to a face as we’re rendering triangles.

As noted, the drawing code works totally fine on GLUT. It works on SDL if I disable VBO’s and render using vertex arrays. It’s only when it uses VBO’s then it I get the error.

LeonardOn 2010-11-03, at 9:56 AM, Ian Mallett wrote:

Hi,
On Wed, Nov 3, 2010 at 10:38 AM, Alex Barry <alex.barry at gmail.com> wrote:
App dies here -----> glDrawElements(GL_TRIANGLES, 3*this->mesh->numFaces, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
This looks funky to me. I think you mean:
glDrawElements(GL_TRIANGLES, this->mesh->numFaces, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
Ian


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Check to see whether glDrawElements is a null function pointer.On Wed, Nov 3, 2010 at 1:09 PM, Leonard Teo <teo.leonard at gmail.com> wrote:

Thanks for checking, but I don’t think that’s the problem as there are 3
index values to a face as we’re rendering triangles.
As noted, the drawing code works totally fine on GLUT. It works on SDL if I
disable VBO’s and render using vertex arrays. It’s only when it uses VBO’s
then it I get the error.
Leonard

On 2010-11-03, at 9:56 AM, Ian Mallett wrote:

Hi,
On Wed, Nov 3, 2010 at 10:38 AM, Alex Barry <alex.barry at gmail.com> wrote:

? ? ? ? ? ? ?? App dies here -----> glDrawElements(GL_TRIANGLES,
3*this->mesh->numFaces, GL_UNSIGNED_INT, BUFFER_OFFSET(0));

This looks funky to me.? I think you mean:
glDrawElements(GL_TRIANGLES, this->mesh->numFaces, GL_UNSIGNED_INT,
BUFFER_OFFSET(0));
Ian


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Nope it’s not a null function pointer. glDrawElements runs fine when I don’t use Vertex Buffer Objects.

LeonardOn 2010-11-03, at 11:17 AM, Justin Coleman wrote:

Check to see whether glDrawElements is a null function pointer.

On Wed, Nov 3, 2010 at 1:09 PM, Leonard Teo <@Leonard_Teo> wrote:

Thanks for checking, but I don’t think that’s the problem as there are 3
index values to a face as we’re rendering triangles.
As noted, the drawing code works totally fine on GLUT. It works on SDL if I
disable VBO’s and render using vertex arrays. It’s only when it uses VBO’s
then it I get the error.
Leonard

On 2010-11-03, at 9:56 AM, Ian Mallett wrote:

Hi,
On Wed, Nov 3, 2010 at 10:38 AM, Alex Barry <alex.barry at gmail.com> wrote:

           App dies here -----> glDrawElements(GL_TRIANGLES,

3*this->mesh->numFaces, GL_UNSIGNED_INT, BUFFER_OFFSET(0));

This looks funky to me. I think you mean:
glDrawElements(GL_TRIANGLES, this->mesh->numFaces, GL_UNSIGNED_INT,
BUFFER_OFFSET(0));
Ian


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Hi Leonard,

I’ve taken a look at GL_ARB_vertex_buffer_object, but that did not reveal any
errors in your code to me. However, you did not show the part where you are
calling glBufferData to fill the buffers.

Anyway, I have two theories:

#1
GLUT and SDL might use a different setup of the screen or window, causing
different GL implementations to be used. For example, I’ve learned that on my
Windows system, requesting 32 bits of depth buffer causes a fallback to MS’
software GL. NVidia’s hardware GL will only work up to 24 bits of depth (at
least for my video card and driver).
I do not know whether something similar is possible on OSX.

If different GLs are at work, the one enabled by GLUT might not support VBOs.
The program would then just run the “else” code, which we know doesn’t fail.

So, unless you already did, try and verify that the VBO code is actually called
when using GLUT.

#2
If VBO support is present in both cases, try checking glError after every GL
function call.

For example, if glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, …) failed, no buffer
object would be bound. Then glDrawElements would have to use BUFFER_OFFSET(0),
which evaluates to a NULL pointer, as a regular pointer to the index data in
system memory. It would thus dereference a NULL pointer.
And although I don’t know much about OSX, EXC_BAD_ACCESS sure sounds like it is
the expected result of dereferencing a NULL pointer.

MartinOn 03.11.2010 17:07, Leonard Teo wrote:

Hi guys,

I wrote a simple application using GLUT that I’m porting to SDL now to turn it into a game.

I am having a bizarre issue specifically with using glDrawElements and Vertex Buffer Objects, SDL 1.2.14 OSX. If I don’t use VBO’s the program runs fine. It only throws a “EXC_BAD_ACCESS” when using VBO’s. To make the matter more obscure. The program runs totally fine in GLUT. Am I missing something?

Any help would be most appreciated.

Thanks,

Leonard

Here’s the drawing code:

if (glewGetExtension(“GL_ARB_vertex_buffer_object”))
{
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

  //Load vertices
  glBindBuffer(GL_ARRAY_BUFFER, this->mesh->vbo_vertices);
  glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
  
  //Load normals
  glBindBuffer(GL_ARRAY_BUFFER, this->mesh->vbo_normals);
  glNormalPointer(GL_FLOAT, 0, BUFFER_OFFSET(0));

  //Load UVs
  glBindBuffer(GL_ARRAY_BUFFER, this->mesh->vbo_uvs);
  glTexCoordPointer(2, GL_FLOAT, 0, BUFFER_OFFSET(0));

  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->mesh->vbo_index);
  App dies here ----->  glDrawElements(GL_TRIANGLES, 3*this->mesh->numFaces, GL_UNSIGNED_INT, BUFFER_OFFSET(0));

  glDisableClientState(GL_VERTEX_ARRAY);
  glDisableClientState(GL_NORMAL_ARRAY);
  glDisableClientState(GL_TEXTURE_COORD_ARRAY);

} else {

  //BTW: If I run this block of code instead of the above, everything renders fine. App doesn't die.

  //Drawing with vertex arrays
  glEnableClientState(GL_VERTEX_ARRAY);
  glEnableClientState(GL_NORMAL_ARRAY);
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);

  glVertexPointer(3, GL_FLOAT, 0, this->mesh->vertexArray);
  glNormalPointer(GL_FLOAT, 0, this->mesh->normalsArray);
  glTexCoordPointer(2, GL_FLOAT, 0, this->mesh->uvArray);

  glDrawElements(GL_TRIANGLES, 3*this->mesh->numFaces, GL_UNSIGNED_INT, this->mesh->indexArray);

  glDisableClientState(GL_VERTEX_ARRAY);
  glDisableClientState(GL_NORMAL_ARRAY);
  glDisableClientState(GL_TEXTURE_COORD_ARRAY);

}

Here’s the debug information:

Program received signal: ?EXC_BAD_ACCESS?.

Thread-1-<com.apple.main-thread>
#0 0x17747a93 in gleRunVertexSubmitImmediate
#1 0x1774772c in gleLLVMArrayFunc
#2 0x177476e4 in gleSetVertexArrayFunc
#3 0x1773073c in gleDrawArraysOrElements_ExecCore
#4 0x176baa7b in glDrawElements_Exec
#5 0x97524050 in glDrawElements

asm gleRunVertexSubmitImmediate

0x17747a93<+0771> mov (%eax,%ecx,4),%eax <-- the app dies on this.

Here’s my SDL initialization code:

//Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO)< 0)
{
cout<< “Could not initialize SDL”<< endl<< SDL_GetError();
exit(2);
}

//Set window
SDL_WM_SetCaption(“Hello World!”, “Hello World!”);

//Set openGL window
if ( SDL_SetVideoMode(width, height, 32, SDL_OPENGL | SDL_RESIZABLE) == NULL ) {
cout<< “Unable to create OpenGL context: %s\n”<< endl<< SDL_GetError();
SDL_Quit();
exit(2);
}

//Set up event handling
SDL_Event event;
bool quit = false;

//Initialize GLEW
GLenum err = glewInit();
if (GLEW_OK != err)
{
//Problem: glewInit failed, something is seriously wrong.
fprintf(stderr, “Error: %s\n”, glewGetErrorString(err));
exit(1);
}
fprintf(stdout, “Status: Using GLEW %s\n”, glewGetString(GLEW_VERSION));


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org