Results 1 to 3 of 3

Thread: Basic SDL + OpenGL segfault problem.

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2011
    Posts
    2

    Basic SDL + OpenGL segfault problem.

    Hello everyone. I have what seems to me (a beginner) a very strange problem with OpenGL. This is possibly related to SDL or maybe even something else.

    I am writing a class for doing pretty basic 2D drawing with hardware acceleration for a game idea. I had something similar working on another system and rather than rebuild it on my new laptop I decided to write again from ground up. The fact I had no probs on the other machine is driving me nuts.
    Initially I thought it was my usage of glBindTexture() but I have since found that if I try to access ANY variable outside the scope of the drawing methods I get a segmentation fault.

    I am using xubuntu 11.10 and SDL 1.2

    2.1 Mesa 7.11
    Tungsten Graphics, Inc
    Mesa DRI Intel(R) Ironlake Mobile x86/MMX/SSE2

    I initialized SDL with
    Code :
    SDL_Init (SDL_INIT_EVERYTHING)
    display = SDL_SetVideoMode(video_width,video_height,32,SDL_OPENGL )
    then I have an init() function in my OpenGL class like this,
    Code :
     bool init(int video_width, int video_height)
      {
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
        glMatrixMode(GL_PROJECTION);
        glDisable(GL_DEPTH_TEST);
        glLoadIdentity();
        glOrtho(0, video_width, video_height, 0, -1, 1);
        glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
        if(glGetError() != GL_NO_ERROR)
          return false;
     
        return true;
      }

    This works fine with the cout commented out
    Code :
     void rect_fill(float x1, float y1, float x2, float y2, float r, float g, float b)
      {
        //std::cout <<  bl_bitmaps[0]->GL_id << "\n";
        glColor4f(r,g,b,1.0f);
        glBegin(GL_QUADS);
          glVertex2f(x1,y1); 
          glVertex2f(x2,y1);  
          glVertex2f(x2,y2);
          glVertex2f(x1,y2);
        glEnd();
      }
    This segfaults
    Code :
    void rect_fill(float x1, float y1, float x2, float y2, float r, float g, float b)
      {
        std::cout <<  bl_bitmaps[0]->GL_id << "\n";
        glColor4f(r,g,b,1.0f);
        glBegin(GL_QUADS);
          glVertex2f(x1,y1); 
          glVertex2f(x2,y1);  
          glVertex2f(x2,y2);
          glVertex2f(x1,y2);
        glEnd();
      }
    It doesn't matter what that cout is accessing, I also tested with a simple int within the same class and it segfaults on that too.Can anyone shed a little light on what may be happening here? I would be very grateful.

  2. #2
    Newbie Frequent Contributor
    Join Date
    Mar 2016
    Posts
    896

    Re: Basic SDL + OpenGL segfault problem.

    Very hard to say from just the code you posted, some ideas:

    - do you actually create an object of "your opengl class"?

    Code :
    MyOpenGL *ogl;
    ogl->rect_fill(/* ... */);

    the above may compile, and even though ogl is not initialized may work if you don't access any members of the class (because then "this" is unused and nothing notices that it is invalid).
    It should of course be:

    Code :
    MyOpenGL *ogl = new MyOpenGL;
    ogl->rect_fill(/* ... */);

    - run this under a debugger to find the exact spot where it crashes, what it attempts to access, and what the values of the involved variables are.

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2011
    Posts
    2

    Re: Basic SDL + OpenGL segfault problem.

    Thanks for the quick response carsten and appologies for my delay. Down to new year, familiy visits etc, anyway you were spot on.
    I had somehow managed to attempt to use 2 ogl* objects and only created one, which I never even used. Doh! Looked through it again and again and I just couldn't see the problem. I knew it would be something embarrassingly stupid but I never saw it until you pointed it out so thanks very much.

Similar Threads

  1. Opengl segfault
    By benburch9 in forum OpenGL: Linux
    Replies: 5
    Last Post: 01-12-2017, 10:06 AM
  2. SDL and OpenGL extensions causing a segfault
    By WIld Sage in forum OpenGL: General
    Replies: 2
    Last Post: 10-21-2012, 10:26 AM
  3. segfault in multithread OpenGL app on i915
    By Nickolay Kolchin in forum OpenGL: Linux
    Replies: 9
    Last Post: 10-04-2006, 08:34 AM
  4. pthreads + openGL + tcl = segfault!
    By giampiplus in forum OpenGL: Linux
    Replies: 4
    Last Post: 03-18-2003, 01:28 AM
  5. pthread + tcl + openGL = segfault!
    By giampiplus in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 03-14-2003, 09:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean