NeHe Lesson 1: Window Issue

Started by
2 comments, last by joe_bubamara 14 years, 8 months ago
The following segment is from lesson 1, in the method GLvoid KillGLWindow(GLvoid)

if (hRC)											// Do We Have A Rendering Context?
	{
		if (!wglMakeCurrent(NULL,NULL))					// Are We Able To Release The DC And RC Contexts?
		{
			MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		}

		if (!wglDeleteContext(hRC))						// Are We Able To Delete The RC?
		{
			MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);


		}

		hRC=NULL;	// Set RC To NULL									
		
	}

I wanted to know why you try to release hRC and then try to delete hRC and then set it to NULL anyways in that final statment. Like whether you can or can't release/delete hRC it's still set to null so why are these checks done at all? Can't you simply jst get rid of that check code and jst have hRC = NULL? By gettin rid of the check code i mean simply have

if(hRC) {
  hRC = NULL;
}

I've jst started learning this stuff so i'm pretty noob at how this all works. Thanx.
Advertisement
Depending on your needs, SDL might actually be easier to learn alongside your OpenGL lessons. SDL is even easier than GLUT and is still currently supported (with GLUT you have to go after some derivative, like freeglut). It also means you wont have to deal with all that annoying windows code, and it frees you (at least in that regard) from platform specificity.

Anyway, maybe I've misunderstood you, but it sounds like you're confusing pointers with data. Think about a warehouse. If you put something in the warehouse (data), you wont know where it is without a log (pointer). If you put something in, then erase the log (set the pointer to null) the thing is still in the warehouse. Setting a pointer to NULL doesn't delete anything. First you need to take the thing out of the warehouse, then update the log.

In other words, if you don't call delete first, then you've created a memory leak.
Quote:Original post by Zouflain
Anyway, maybe I've misunderstood you, but it sounds like you're confusing pointers with data. Think about a warehouse. If you put something in the warehouse (data), you wont know where it is without a log (pointer). If you put something in, then erase the log (set the pointer to null) the thing is still in the warehouse. Setting a pointer to NULL doesn't delete anything. First you need to take the thing out of the warehouse, then update the log.


That has got to be the single best description of the problem.
It is common to sett all pPointers to 0, after calling "delete" or "free" on them, to ensure that some code wouldn't eventually try to use them.

It is usual in C and C++ to write code like:

if(ptr){
// do something ....
}

which means if ptr != 0 than do something.

In case ptr is deleted or freed, it will point to not-existent data, but the code in if-statement would still execute. For that reason it is advised to always sett a pointer to zero after calling free or delete.

Obviously in case of that example it is overkill of safety, but it is good to be consistent in coding.

This topic is closed to new replies.

Advertisement