Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8278051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:52:32+00:00 2026-06-08T08:52:32+00:00

My code works fine when using glDrawArrays, but whenever I make an index buffer

  • 0

My code works fine when using glDrawArrays, but whenever I make an index buffer and change it to use glDrawElements, it segfaults.

(Strangely, not before or after the drawing…but when the main loop exits !)

Here is the simple code..where I declare 4 vertices, and 3 indices to them for drawing a triangle.

int main()
{
    sf::Window win(sf::VideoMode(400,400,32),"Manasij");
    GlewInit();
    mm::Program program
    (
        {
            mm::Shader(GL_VERTEX_SHADER,"vshader.vert"),
            mm::Shader(GL_FRAGMENT_SHADER,"fshader.frag")
        }
    );
    float vdata[]=
    {
        0.0f,0.0f,
        0.5f,0.0f,
        0.5f,0.5f,
        0.0f,0.5f
    };
    GLubyte indices[]={0,1,3};

    GLuint vao,vbo,ebo;
    glGenVertexArrays(1,&vao);
    glBindVertexArray(vao);

    glGenBuffers(1,&vbo);
    glBindBuffer(GL_ARRAY_BUFFER,vbo);
    glBufferData(GL_ARRAY_BUFFER,8*sizeof(float),vdata,GL_STATIC_DRAW);

    glGenBuffers(1,&ebo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ebo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,3*sizeof(GLubyte),indices,GL_STATIC_DRAW);

    glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,0,0);
    glEnableVertexAttribArray(0);
    glBindAttribLocation(program.getHandle(),0,"pos");
    //glBindVertexArray(0);

    glUseProgram(program.getHandle());
    //glBindVertexArray(vao);
    glClearColor(1.0f,1.0f,1.0f,1.0f);

    while(win.isOpen())
    {
        sf::Event eve;
        while(win.pollEvent(eve))
            if(eve.type==sf::Event::Closed)
                win.close();
        glClear(GL_COLOR_BUFFER_BIT);

//      glDrawArrays(GL_TRIANGLES,0,3);
        glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_BYTE,(GLvoid*)0);
        win.display();  
    }
    return 0;
}

And the shaders are as simple as they get:

#version 330
in vec2 pos;
void main()
{
    gl_Position = vec4(pos,0.0f,1.0f);
}

and

#version 330
void main()
{
    gl_FragColor = vec4(1.0f,0.3f,0.8f,1.0f);
}

And gdb shows :

Breakpoint 1, main () at test.cpp:57
57                      win.display();
(gdb) continue 
Continuing. //<- At this point I try to close the window, thus triggeing the main loop to break

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff557d164 in ?? () from /lib/libnvidia-glcore.so.302.17

Any idea what I’m botching up ?

P.S: If anyone wants to test, here an archive..complete with a makefile!

https://docs.google.com/file/d/0B0oubbp-MVYOeGFuX0owXzhRTEU/edit

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:52:33+00:00Added an answer on June 8, 2026 at 8:52 am

    The problem was that in the last iteration of the loop, when the ‘close’ event was detected, the window was signaled the close, but even after that I was calling glDrawElements for one last time.
    Since everything was already gone, that resulted in a segfault.

    The solution is to put a break statement after win.close() is called or to put the event handling code to the end of the loop.

    (Thanks to cmtptr from the archlinux forums for catching this, I was almost convinced that this was a driver problem)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this simple code, using Joda-time. Works fine, but I have a problem.
This following code snippet works fine using jQuery1.2.3, but it doesn’t work with latest
The following code works fine with python.exe but fails with pythonw.exe. I'm using Python
I'm using core motion in my app. This code works fine: motionManager = [[CMMotionManager
I've written some code using the REST starter kit and it works fine on
I've got this toy code, works fine, using MySQL var r = new SimpleRepository(DB,
This code works fine, but I'll want to handle exception if any thing goes
This code works fine to find an available room within certain date, but it
This code works fine in C#: Expression.Lambda(LambdaBody); But none of the methods for building
My code works fine when I call AA.sendRequest() from the console, but not when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.