Archived

This topic is now archived and is closed to further replies.

Eber Kain

Problem : Have to reboot after running my gl app, before I can run it again

Recommended Posts

I get an error on this line wglMakeCurrent(*hdc,*hrc) and with a query to GetLastError() i get ERROR_ALREADY_EXISTS My delete window code is this ChangeDisplaySettings(NULL,0); wglMakeCurrent(NULL,NULL); wglDeleteContext(*hrc); ReleaseDC(*hwn,*hdc); DestroyWindow(*hwn); ShowCursor(TRUE); UnregisterClass("OpenGL",*hin); Dose anyone see the problem?

Share this post


Link to post
Share on other sites
quote:
Original post by Eber Kain
wglDeleteContext(*hrc);
ReleaseDC(*hwn,*hdc);
DestroyWindow(*hwn);
ShowCursor(TRUE);
UnregisterClass("OpenGL",*hin);



I don''t think, if you look in NeHe tutorial 1, the hwn, hdc, hrc or hin variables should have the *s. Try this:
  
wglDeleteContext(hrc);
ReleaseDC(hwn,hdc);
DestroyWindow(hwn);
ShowCursor(TRUE);
UnregisterClass("OpenGL",hin);


I think what you did the first time was release and destroy whatever the pointer was pointing to, rather than the pointer itself. I may be wrong but I think the above code will work

Baldur K.

Share this post


Link to post
Share on other sites
NeHe actually calls ChangeDisplaySettings(NULL,0) between DestroyWindow and ShowCursor. Moreover, he only calls it when the application ran in fullscreen mode.
Otherwise, everything seems to be correct.
You should test what returns every function, but that''s a detail.

Share this post


Link to post
Share on other sites
I do test everything, and I am not useing NeHe''s code. Those *''s are supposed to be there. I will post all my window code for clarity. It worked before I added all the pointer stuff, perhaps its not seeing "OpenGL" as a class name since its declared dynamically now, I will test that when I reboot again.

bool glhs::CreateWin(short p_w, short p_h, int p_b) {
*hdc=NULL;
*hwn=NULL;
*hrc=NULL;

//assign the width and height to variables
*width = p_w;
*height = p_h;
*bits = p_b;

//Init the display lists
Init();

//Set the windows desired size
(*rect).left = 0;
(*rect).right = *width;
(*rect).top = 0;
(*rect).bottom = *height;

//Grab an instance for the window
if(!(*hin = GetModuleHandle(NULL))) error[0] = true;

//Setup the window style
(*wc).cbClsExtra = 0;
(*wc).cbWndExtra = 0;
(*wc).hbrBackground = NULL;
(*wc).hCursor = NULL;
(*wc).hIcon = NULL;
(*wc).hInstance = *hin;
(*wc).lpfnWndProc = (WNDPROC) WndProc;
(*wc).lpszClassName = "OpenGL";
(*wc).lpszMenuName = NULL;
(*wc).style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;

//Register the window class
if(!RegisterClass(wc)) error[1] = true;

//Setup device mode
(*ss).dmSize = sizeof(*ss);
(*ss).dmPelsHeight = *height;
(*ss).dmPelsWidth = *width;
(*ss).dmBitsPerPel = *bits;
(*ss).dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;

//go into fullscreen mode
int errorcode = ChangeDisplaySettings(ss,CDS_FULLSCREEN);
switch(errorcode) {
case DISP_CHANGE_RESTART:
error[2] = true;
break;
case DISP_CHANGE_BADFLAGS:
error[3] = true;
break;
case DISP_CHANGE_BADPARAM:
error[4] = true;
break;
case DISP_CHANGE_FAILED:
error[5] = true;
break;
case DISP_CHANGE_BADMODE:
error[6] = true;
break;
case DISP_CHANGE_NOTUPDATED:
error[7] = true;
break;
}

//Adjust the window rect
AdjustWindowRectEx(rect,WS_POPUP,false,WS_EX_APPWINDOW);

//Create the window
if(!(*hwn = CreateWindowEx(WS_EX_APPWINDOW,"OpenGL","OpenGL Helper Library",WS_POPUP,
0,0,rect->right-rect->left, rect->bottom-rect->top,
NULL,NULL,*hin,NULL)))
error[8] = true;

//Setup a Pixel Format Descriptor
static PIXELFORMATDESCRIPTOR pfd = {sizeof(PIXELFORMATDESCRIPTOR),1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA,
p_b,0,0,0,0,0,0,0,0,0,0,0,0,0,*depthbuffer,0,0,PFD_MAIN_PLANE,0,0,0,0};

//Get the device context
if(!(*hdc = GetDC(*hwn))) error[9] = true;

//Get a pixel format
if(!(*pf = ChoosePixelFormat(*hdc,&pfd))) error[10] = true;

//Set the pixel format
if(!SetPixelFormat(*hdc,*pf,&pfd)) error[11] = true;

//Get a rendering context
if(!(*hrc = wglCreateContext(*hdc))) error[12] = true;

//Activate the rendering context
if(!wglMakeCurrent(*hdc,*hrc)) error[13] = true;

//Show the window
ShowWindow(*hwn,SW_SHOW);
SetForegroundWindow(*hwn);
SetFocus(*hwn);
ShowCursor(FALSE);

//If any errors are true write error codes
bool fileopen = false, quit = false;
for(int i = 0; i < 14; i++) {
if(error == true) {
if(!fileopen) {
glh_ofile.open(GLH_ERROR_FILE_NAME);
fileopen = true;
};
quit = true;
WriteError(i);
}
}
if(quit) {
DestroyWin();
return false;
}
return true;
}

void glhs::DestroyWin() {
ChangeDisplaySettings(NULL,0);
wglMakeCurrent(NULL,NULL);
wglDeleteContext(*hrc);
ReleaseDC(*hwn,*hdc);
DestroyWindow(*hwn);
ShowCursor(TRUE);
UnregisterClass("OpenGL",*hin);
}

Share this post


Link to post
Share on other sites


ALWAYS SetLastError(0); when a function returns ok. Windows will report erros when they havn''t happened A LOT. and it differes greatly depending on the hardware. In my app, if I don''t do this, I can accululate over 40 windows errors this way just by opening and closing the app, but the app still works without error, or memory leak, etc.

I can get everything from invalid handles, and out of memory (when I have 384mb) to insane things like a corrupt bitmap file... (when I''m not even using them yet)...

and, as I said, it differs on different hardware.



ok:

a minor thing:

(*wc).blah();
write it like this:

wc->blah();

are your hdc, hwn and hrc objects double pointers?

seems very odd to be doing:

*hdc=NULL;

Share this post


Link to post
Share on other sites
ok... when I say 40, thats because I test after every major call..

and they sometimes even occur between calls... which is nuts (ie, GetLastError is causing an error)...

also note that nehes base code makes the exact same errors. so it is not an issue with the code, it''s a windows thing.

Share this post


Link to post
Share on other sites