Question

I am trying to use OpenGL with shared context (because of sharing textures between windows) via FreeGLUT library... It work fine, I can share textures, but i failed on the end of program or during windows closing by mouse...

I have cerated the code which emulate the problem: (http://pastie.org/9437038)

// file: main.c
// compile: gcc -o test -lglut main.c
// compile: gcc -o test -lglut -DTIME_LIMIT main.c

#include "GL/freeglut.h"

#include <unistd.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
  int winA, winB, winC;
  int n;

  glutInit(&argc, argv);

  glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE , GLUT_ACTION_CONTINUE_EXECUTION);
  //glutSetOption(GLUT_RENDERING_CONTEXT, GLUT_USE_CURRENT_CONTEXT);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  winA = glutCreateWindow("Test A");
  glutSetOption(GLUT_RENDERING_CONTEXT, GLUT_USE_CURRENT_CONTEXT);
  winB = glutCreateWindow("Test B");
  winC = glutCreateWindow("Test C");

  printf("loop\n");
  #ifdef TIME_LIMIT
  for (n=0;n<50;n++)
  {
    glutMainLoopEvent();

    usleep(5000);
  }
  #else //TIMELIMIT
  glutMainLoop();
  #endif // TIME_LIMIT

  printf("Destroy winC\n");
  glutDestroyWindow(winC);
  printf("Destroy winB\n");
  glutDestroyWindow(winB);
  printf("Destroy winA\n");
  glutDestroyWindow(winA);

  printf("Normal end\n");

  return 0;
}

Output:

loop
X Error of failed request:  GLXBadContext
  Major opcode of failed request:  153 (GLX)
  Minor opcode of failed request:  4 (X_GLXDestroyContext)
  Serial number of failed request:  113
  Current serial number in output stream:  114
Segmentation fault

output with TIME_LIMIT:

loop
Destroy winC
Destroy winB
Destroy winA
Segmentation fault

Without calling glutSetOption(GLUT_RENDERING_CONTEXT, GLUT_USE_CURRENT_CONTEXT);, it works well.

Do anybody have idea what am I doing bad?

Was it helpful?

Solution

The option GLUT_USE_CURRENT_CONTEXT does not create shared contexts. It just means that the same GL context is used for all windows. You only have one GL conxtext, and destroy it when you first destroy a window which uses that, so the other destruction calls fail. None of the GLUT implementations I'm aware of actually supports GL context sharing.

GLUT_USE_CURRENT_CONTEXT is more like a hack (and it is nor part of the GLUT specification anyway), and not really a well-implemented. It could use some reference counting to destroy the context not before the last window using it is destroyed, but that is simply not the case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top