Help: solve GLEW failure with GLX

I cannot get a minimal program based on GLEW, GLX, xlib and OpenGL to work.

If I don’t create a GLX/OpenGL context, glewInit() returns an error (no OpenGL version).

If I try to create a GLX/OpenGL context before calling the glewInit() function, the program blows up in glXCreateContext(). Specifically, my code calls the glXCreateContext() function, but that function terminates/exits the application and never returns.

I’m at a loss at this point. My code is based upon other code I’ve found on the OpenGL wiki and elsewhere, and is very simple. Also note that my large program from which this minimal piece was extracted was running fine until I switched from GLEE to GLEW (because GLEE has fallen behind and I need at least OpenGL v3.20).

Also note that I compile glew.c into my program rather than mess with libraries and such.

I am developing this application on CodeBlocks on the 64-bit version of ubuntu v10.04 (but compiling a 32-bit mode app).

I attach a ZIP file that contains all the files, including the CodeBlocks workspace/project file, which makes it easy to load and test if you have CodeBlocks on linux.

Any ideas? This should work, but doesn’t.

OS: ubuntu v10.04 : 64-bit version
IDE: Code::Blocks w/GNU compilers
CPU: AMD Phenom II x4 + 16GB RAM
GPU: nvidia GTX9600
driver: 195.36.24
GLEW: v157

You are calling glew init without making the context active.
Add this after the context creation
glXMakeCurrent(xdisplay, xwindow, glxcontext);

also is not a good idea to do this:
xsetwindowattributes.colormap = xcolormap;
and then create the xcolormap

ColorMap is defined as XID that is a long int I got Bad Color map

Well, that’s fine, but I guess you didn’t notice that calling the function glXCreateContext() terminates the application!

Obviously the context must be created before it is “made current”, so while that may be relevant once I can get the code to execute past glXCreateContext(), it is not relevant to my problem.

Any other ideas?

Can you execute this code past glXCreateContext()? If so, what is your OS, CPU, video card, driver version, etc?

No it work fine for me after I solved the problem with the color map and glXmakeCurrent.

I didn’t used the glew you include in the zip, just the one that came with Ubuntu.

My system is Ubuntu 10.10 64bit

CPU Q6600 (why do you think the CPU is important?)

nVidia 460 GTX 768Mb with nVidia driver 260.19.29

Kernel 2.6.35-24
Xorg 1.9.0

I have used only library that came with the distribution.

I had the same problem with GLEW v155 before I tried GLEW v157, which is the one in the ZIP file. If you have a version of GLEW before v155, could you try again with the GLEW files in the ZIP file I attached? I’d appreciate that.

If I still cannot get this to work after 1 more day, I will install ubuntu64 v10.10 since that works for you.

However, before I do that, I will probably do a complete new clean install of ubuntu64 v10.04 first on the chance something in my system got tweaked somehow.

Even before that, I’m tempted to install nvidia driver 260.19.29 (which I have already downloaded, but not installed). I am somewhat fearful of trying to manually install the nvidia driver, because many people have caused disasters trying this, and because my linux internals and administration skills are rather lame. Can you tell me the process you went through to accomplish this? Or, if the following is what you did, just say “that works” (to save you time):

I very much doubt the CPU makes any difference. Including that information was just being overly thorough.

Thanks very much for taking the time to help me with this. You’re feedback is very helpful and appreciated.

Before doing it “manually”, try this method first :

Thanks much for that link. The process it described worked perfectly, and installed the latest v260.29.19 driver.

Ha!!! I should have taken your comments more seriously!

I switched around the code so xcolormap was created before being assigned to xsetwindowattributes.colormap (before XCreateWindow() is called). After that, calling glXCreateContext() no longer terminated the application.

Clearly glXCreateContext() definitely shouldn’t react so dramatically (it should return an error, not terminate the application), but… there we go, huh?

So now that this all works, let me make sure I understand the cause of this whole fiasco.

Before glXChoseFGConfig() and many other newer GLX functions can be called (to prepare to create a GLX/OpenGL context), their addresses must be established, which is done with glewInit() is called.

BUT, GLEW + X/GLX/OpenGL have a chicken and egg problem, because [for some reason unknown to me] GLEW cannot initialize itself until a X/GLX/OpenGL context has been created.

To solve this chicken and egg problem, we must create an X window and its GLX/OpenGL context with the original GLX v1.1 functions, which are always valid. Then we can call glewInit() to assign addresses to all the v1.2 ~ v1.5 GLX functions, at which time GLX/OpenGL should work “correctly”.

Therefore, this silly code to create an X window, create the GLX/OpenGL context, initialize GLEW by calling glewInit(), then deleting the context, window and so forth… should be performed in the application initialization process.

Then everywhere in the “real application” the newer, better GLX functions get called.


BUT, GLEW + X/GLX/OpenGL have a chicken and egg problem, because [for some reason unknown to me] GLEW cannot initialize itself until a X/GLX/OpenGL context has been created.

it’s just like in windows. you can’t expect to get extension pointers without an active opengl context.

Definitely :stuck_out_tongue:

When I compiled your program I get the error
“Invalid color map” that’s why spotted the error quite easily.
Try to install the develop library for X and compile with debug symbol.

Usually you don’t link all the GLX function, but the same problem arise when you have to initialize an openGl context with version > 2.1
Your procedure is almost correct
1 - The only additional Glx function that you need is glxCreateContext that you can manually retrieve, so you don’t need to call glewInit two times.
2 - If you don’t need openGL version >2.1 you need only a context
enjoy with your gl 2.1 context
if you need openGL >= 3.0
get createContextAttribARB
create a new context with the new function
delete the old context
enjoy with your gl 3.x context

Basically you are linking a library that let you load another library that you need a context (that open the opengl library) and to retrieve the function manually (GLEW do this work for you).
This can sound strange but you are communicating with an hardware that can expose different functionality and this functionality can’t be present or not.
If you have a static openGl library when you update the driver you need to compile again all your program. With other hardware (mouse, keyboard) this work is done by the operative system, but you don’t want to use a system call for every openGl function this will be utterly slow, also you need a new version of X for every new openGL function. So this is the most efficient / versatile solution.

Yeah, and I didn’t get the “invalid colormap” error, which might have saved me some time if I had.

Of course the contents of the xcolormap assigned to that other structure will be different on our machines, because xcolormap was a local [structure] variable that I did not clear after I created it (bad practice error #1).

So its contents will be whatever jibberish is on our stacks, which will definitely not be the same. I guess my jibberish was not distinguishable from a valid xcolormap. Bummer! And likewise XCreateWindow() and XCreateContext() could (or did) not detect the bogusness of xcolormap on my computer either, which led to XCreateWindow() accepting the bogus xcolormap, and XCreateContext() fatally trying to “run with it”.

I mostly understand the problem now, but I must say, I’ve read 39 million pages of XWindows/GLX/OpenGL documentation and book-descriptions and forum-messages and so forth… and do not recall ever seeing a fully coherent statement of “this issue”. Bits and pieces here and there… yes.

I think I pretty much understand now, but what a mess. I am inclined to post my final routines in this thread so others might find them, and hopefully save themselves some pain.

In my application, any number of windows can be created. So the best approach (and what I finally implemented) was to simply go through this process in an “initialize” function, but never map the window, and destroy/free the window, context and colormap in that function.

Then in the normal operation of the application, the code can call the latest and greatest GLX and OpenGL functions when it creates windows, contexts, etc. And yes, the only place in this scheme for glewInit() is in the initialization routine.

Sound right? I’ll try to listen more carefully this time. :-o

Can you give example how to make Opengl context 3.x
because my attempts are great fail
And cant draw anything on the screen

Next time, please start a new thread.

Check out the first snippet here:

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.