# SetPixelFormat freezes

**URL:** <https://community.khronos.org/t/setpixelformat-freezes/23495>\
**Category:** OpenGL: Basic Coding\
**Created:** [December 25, 2002, 2:18pm UTC](https://community.khronos.org/t/setpixelformat-freezes/23495 "2002-12-25T14:18:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cardinal](https://community.khronos.org/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@Cardinal](https://community.khronos.org/u/Cardinal)\
**Post date:** [December 25, 2002, 2:18pm UTC](https://community.khronos.org/t/setpixelformat-freezes/23495/1 "2002-12-25T14:18:00Z")

</div>

running the following initialization code, the program will cause my computer to freeze when executing SetPixelFormat(…). I have not tested it on other system, and if anyone else could, that would be awesome. Here is .c file contents:

> ```auto
> 
> ```

> #include \<stdarg.h\>  
> #include “mgl.h”
> 
> /\*\*\*\*\* Static Prototypes \*\*\*\*\*\*/  
> static boolean createWindow(context \* nglc);  
> static void destroyWindow(context \* nglc);  
> static boolean createGL(context \* nglc);  
> static void destroyGL(context \* nglc);
> 
> void createContext(context \*\*t,int height,int width,int bits,char \*name)  
> {  
> if( t!=NULL && (\*t = (context \*)malloc(sizeof(context))) != NULL )  
> {  
> (\*t)-\>height = height;  
> (\*t)-\>width = width;  
> (\*t)-\>bits = bits;  
> (\*t)-\>hInst = NULL;  
> (\*t)-\>hWnd = NULL;  
> (\*t)-\>hDC = NULL;  
> (\*t)-\>hRC = NULL;  
> (\*t)-\>name = (name!=NULL?strdup(name):strdup(“”));  
> }  
> }
> 
> void deleteContext(context \*\*t)  
> {  
> if( (t != NULL) && (\*t != NULL) )  
> {  
> if( (\*t)-\>name != NULL )  
> free((\*t)-\>name);  
> free(\*t);  
> \*t = NULL;  
> }  
> }
> 
> boolean initGL(context \* nglc)  
> {  
> if(createWindow(nglc) && createGL(nglc))  
> return true;  
> killGL(nglc);  
> return false;  
> }
> 
> void killGL(context \* nglc)  
> {  
> destroyGL(nglc);  
> destroyWindow(nglc);  
> }
> 
> /\* Creates a window for use by initGL \*/  
> static boolean createWindow(context \*nglc)  
> {  
> WNDCLASSEX wcx;  
> DEVMODE devmode;
> 
> memset(&wcx,0,sizeof(WNDCLASSEX));  
> wcx.cbSize = sizeof(WNDCLASSEX);  
> wcx.style = CS\_OWNDC;  
> wcx.lpfnWndProc = msgHandler;  
> if( (wcx.hInstance = (nglc-\>hInst = GetModuleHandle(NULL))) == NULL) return false;  
> wcx.lpszClassName = “mglClass”;
> 
> memset(&devmode,0,sizeof(DEVMODE));  
> devmode.dmSize = sizeof(devmode);  
> devmode.dmPelsWidth = nglc-\>width;  
> devmode.dmPelsHeight = nglc-\>height;  
> devmode.dmBitsPerPel = nglc-\>bits;  
> devmode.dmFields = DM\_PELSWIDTH | DM\_PELSHEIGHT | DM\_BITSPERPEL;
> 
> if( RegisterClassEx(&wcx) == 0 ) return false;
> 
> if( (nglc-\>hWnd = CreateWindowEx(WS\_EX\_APPWINDOW,“mglClass”,nglc-\>name,CS\_OWNDC,0,0,nglc-\>width,nglc-\>height,NULL,NULL,nglc-\>hInst,NULL)) == NULL ) return false;
> 
> if( ChangeDisplaySettings(&devmode,0) != DISP\_CHANGE\_SUCCESSFUL ) return false;
> 
> ShowWindow(nglc-\>hWnd,SW\_SHOWNORMAL);  
> ShowCursor(false);  
> SetCapture(nglc-\>hWnd);
> 
> return true;  
> }
> 
> /\* destroys a initGL window \*/  
> static void destroyWindow(context \* nglc)  
> {  
> ReleaseCapture();  
> ShowCursor(true);  
> ChangeDisplaySettings(NULL,0);  
> if( nglc-\>hWnd )  
> {  
> DestroyWindow(nglc-\>hWnd);  
> nglc-\>hWnd = NULL;  
> }  
> if( nglc-\>hInst )  
> {  
> UnregisterClass(“mglClass”,nglc-\>hInst);  
> nglc-\>hInst = NULL;  
> }  
> }
> 
> static boolean createGL(context \* nglc)  
> {  
> PIXELFORMATDESCRIPTOR pfd;
> 
> memset(&pfd,0,sizeof(PIXELFORMATDESCRIPTOR));  
> pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);  
> pfd.nVersion = 1;  
> pfd.dwFlags = PFD\_DRAW\_TO\_WINDOW | PFD\_SUPPORT\_OPENGL | PFD\_DOUBLEBUFFER;  
> pfd.iPixelType = PFD\_TYPE\_RGBA;  
> pfd.cColorBits = nglc-\>bits;  
> /\* pfd.cAccumBits = 0; _/  
> pfd.cDepthBits = nglc-\>bits;  
> /_ pfd.cStencilBits = 0; \*/  
> pfd.iLayerType = PFD\_MAIN\_PLANE;
> 
> if( !(nglc-\>hDC = GetDC(nglc-\>hWnd)) ) return false;  
> if( !SetPixelFormat(nglc-\>hDC,ChoosePixelFormat(nglc-\>hDC,&pfd),&pfd) ) return false;  
> if( !(nglc-\>hRC = wglCreateContext(nglc-\>hDC)) ) return false;  
> if( !wglMakeCurrent(nglc-\>hDC,nglc-\>hRC) ) return false;
> 
> return true;  
> }
> 
> static void destroyGL(context \* nglc)  
> {  
> wglMakeCurrent(NULL,NULL);  
> if( nglc-\>hRC )  
> {  
> wglDeleteContext(nglc-\>hRC);  
> nglc-\>hRC = NULL;  
> }  
> if( nglc-\>hDC )  
> {  
> ReleaseDC(nglc-\>hWnd,nglc-\>hDC);  
> nglc-\>hDC = NULL;  
> }  
> }

> ```auto
> 
> ```

and the accompaning header

> ```auto
> 
> ```

> #ifndef MGLH  
> #define MGLH
> 
> /\*\*\*\*\* include files \*\*\*\*\*\*/  
> #include\<windows.h\>  
> #include\<stdio.h\>  
> #include\<gl/gl.h\>  
> #include\<gl/glu.h\>  
> #include\<gl/glaux.h\>
> 
> /\*\*\*\*\* structures \*\*\*\*\*\*/  
> typedef struct  
> {  
> int height;  
> int width;  
> int bits;  
> HINSTANCE hInst;  
> HWND hWnd;  
> HGLRC hRC;  
> HDC hDC;  
> char \*name;  
> } context;
> 
> #ifndef BOOLEAN  
> #define BOOLEAN  
> typedef enum{false,true} boolean;  
> #endif  
> /\*\*\*\*\* gl prototypes \*\*\*\*\*\*/
> 
> /\* window message handler-MUST BE DEFINED BY USER \*/  
> LRESULT msgHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
> 
> /\* creates a new pointer at t, with preset values: height, width, bits, and name \*/  
> void createContext(context \*\*t,int height,int width,int bits,char \*name);
> 
> /\* deletes a existing context, and frees all associated memory \*/  
> void deleteContext(context \*\*t);
> 
> /\* PRECONDITION: nglc is not NULL
> 
> - initializes and starts a rendering context \*/  
> boolean initGL(context \*nglc);
> 
> /\* PRECONDITION: nglc is not NULL
> 
> - destroys and resets nglc \*/  
> void killGL(context \*nglc);
> 
> #endif

> ```auto
> 
> ```

This is the main .c file:

> ```auto
> 
> ```

> /\*\*\*\*\* global includes \*\*\*\*\*\*/  
> #include \<stdio.h\>
> 
> /\*\*\*\*\* local includes \*\*\*\*\*\*/  
> #include “mgl.h”
> 
> /\*\*\*\*\* typedefs \*\*\*\*\*\*/  
> #ifndef BOOLEAN  
> #define BOOLEAN  
> typedef enum{false,true} boolean;  
> #endif
> 
> /\*\*\*\*\* global static variables \*\*\*\*\*\*/  
> static boolean loop = true;
> 
> /\*\*\*\*\* code \*\*\*\*\*\*/  
> int main(int argc,char \*argv)  
> {  
> context \*nglc;  
> MSG msg;
> 
> createContext(&nglc,768,1024,32,“default”);  
> initGL(nglc);  
> while(loop)  
> {  
> if( PeekMessage(&msg,nglc-\>hWnd,0,0,PM\_REMOVE) )  
> DispatchMessage(&msg);  
> }  
> killGL(nglc);  
> deleteContext(&nglc);  
> return 0;  
> }
> 
> LRESULT msgHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )  
> {  
> switch(uMsg)  
> {  
> case(WM\_KEYDOWN):  
> loop = false;  
> default:  
> break;  
> }  
> return DefWindowProc(hwnd,uMsg,wParam,lParam);  
> }

> ```auto
> 
> ```

Give it a try, and see if it has the same results. Be aware the freeze requires a reboot.

Thanks,  
Cardinal

---

<div class="post-metadata">

**Author:** ![mikael\_aronsson](https://community.khronos.org/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@mikael\_aronsson](https://community.khronos.org/u/mikael_aronsson)\
**Post date:** [December 25, 2002, 11:29pm UTC](https://community.khronos.org/t/setpixelformat-freezes/23495/2 "2002-12-25T23:29:00Z")

</div>

Hi !

A good start would be to check what ChoosePixelFormat() returns, can you run other OpenGL apps without problems ?, just so you don’t have a wacky driver.

I can’t test it, don’t have mgl installed.

Mikael

---

<div class="post-metadata">

**Author:** ![Jan](https://community.khronos.org/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@Jan](https://community.khronos.org/u/Jan)\
**Post date:** [December 26, 2002, 10:26am UTC](https://community.khronos.org/t/setpixelformat-freezes/23495/3 "2002-12-26T10:26:00Z")

</div>

The first thing you should do, is to go to nvidias site and download their pixelformat-demo program. If it crashes too, you have the wrong driver.

Some time ago nVidia released a driver, which had a bug in the pixelformat extension. If you still have this driver, you should download the latest one, the bug is fixed there.

Jan.

---

<div class="post-metadata">

**Author:** ![Cardinal](https://community.khronos.org/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@Cardinal](https://community.khronos.org/u/Cardinal)\
**Post date:** [December 26, 2002, 5:58pm UTC](https://community.khronos.org/t/setpixelformat-freezes/23495/4 "2002-12-26T17:58:00Z")

</div>

I downloaded a random lesson from [nehe.gamedev.net](http://nehe.gamedev.net) and it ran just fine; i’ve also been running opengl games, and other programs without problems before-so I am fairly certain its a code/language dependant problem.

I am compiling the code with lcc-win32, and this is first program I have done outside of Borland Builder.

In the first version, which suffered from the same problem, i had a

> ```auto
> 
> ```

> if( ChoosePixelFormat(…) \<= 0 ) return false;

> ```auto
> 
> ```

but always returned valid formats, and even an invalid format should only make SetPixelFormat return false.

[This message has been edited by Cardinal (edited 12-26-2002).]

---

<div class="post-metadata">

**Author:** ![Cardinal](https://community.khronos.org/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@Cardinal](https://community.khronos.org/u/Cardinal)\
**Post date:** [January 9, 2003, 12:13am UTC](https://community.khronos.org/t/setpixelformat-freezes/23495/5 "2003-01-09T00:13:00Z")

</div>

I am not exactly sure why SetPixelFormat triggered the freezes, but after correctly declaring:

> ```auto
> 
> ```

> LRESULT CALLBACK msgHandler(…)

> ```auto
> 
> ```

the code worked gracefully. G’luck to any of you with similarly strange and painstaking bugs.
