Advertisement

Tutorial No.6: Question about TextureImage

Started by March 12, 2003 12:21 AM
6 comments, last by inrecovery 21 years, 11 months ago
Hi There, I am a newbie to OpenGL and also to this forum. This is my first post and bears the potential of many members remarking at its sheer stupidity!!! he he Nevretheless, here goes the question With reference to Nehe's Tutorial #6(God bless him): Can TextureImage[0]->data exist *independent* of TextureImage??? This is what I mean:In the function LoadGLTextures(), TextureImage[0] is being used to store the return value of the LoadBMP() function.After that, Nehe writes(I quote here)
quote:
Now we free up any ram that we may have used to store the bitmap data. We check to see if the bitmap data was stored in TextureImage[0]. If it was we check to see if the data has been stored. If data was stored, we erase it. Then we free the image structure making sure any used memory is freed up.

      
if (TextureImage[0])	// If Texture Exists

{
	if (TextureImage[0]->data)					// If Texture Image Exists

	{
		free(TextureImage[0]->data);			         // Free The Texture Image Memory

	}

	free(TextureImage[0]);					// Free The Image Structure

}
  
Why do we check for the existence of TextureImage[] and then whether or not TextureImage[]->Data has been set or not? Will the following not suffice?
   
      
if (TextureImage[0])	// If Texture Exists

{
          free(TextureImage[0]->data);

	 free(TextureImage[0]);			
}
      
How is it possible that TextureImage[] has been set to the requisiste texture but TextureImage[0]->Data does not exist? What are the conditions in which this case would arise? Please comment! It seems that I am missing the BIG picture here. "Recovering Thinker" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Learn from the mistakes of others. You can't live long enough to make them all yourself. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [edited by - inrecovery on March 12, 2003 1:22:10 AM] [edited by - inrecovery on March 12, 2003 1:22:57 AM]
I don't know how the glaux library is implemented...
auxDIBImageLoad returns a pointer to AUX_RGBImageRec.
So:
if the function fails, the return value should be null...
TextureImage[0]->data is a pointer too...
So it could be that auxDIBImageLoad didn't allocate any data but it allocated an instance of AUX_RGBImageRec...

Please correct me if its not correct



PM


times change


Excuse my poor english!

[edited by - PM on March 12, 2003 11:19:19 AM]
PM Times change... Excuse my poor english!
Advertisement
The reason is simple:

TextureImage[0] is just a pointer to an AUX_RGBImageRec.

At first, TextureImage[0] points to nothing.

It is always possible that the data member will not be allocated, so you don''t want to do a free() on non-allocated memory. So you check if you have a valid TextureImage[0], then you check if the data pointer was allocated memory, you free it, then you free the memory for the AUX_RGBImageRec structure pointed to by TextureImage[0].

Thanks very much!
I understand now.....I think I overlooked some facts before posting.
Let me restate what I understood to mke things absolutely clear in my mind:
First check of TextureImage[] has been allocated any memory:when the LoadBMP() fails, the if condition will be skipped and the status will be returned.
If instead, we just free TextureImage[]->data without checking for the fact if whether or not TextureImage[] has been allocated any memory, it would indeed be erroneous because we would be freeing *unallocated* memory.
Is that right?
So....the first if namely : if (TextureImage[0]) checks if texture exists and the next if namely: TextureImage[0]->data will check if the data structure member has been allocated or not.
BUT: in what circumstances will one exist independently of the other???
Sorry dont get that......
It probably will never happen but it''s just good programming practice to do it like that.
Thanks!
Let me say this another way:
The reason we use the two if''s is:
--In case LoadBMP() fails, the if condition(fisrt one and consequently also second one) are bypassed and status is returned.
--In case LoadBMP() is successful and the TextureImage[] pointer and consequently the TextureImage[]->Data has been set, the *good* way to delete them would be as in NeHe''s tutorial namely first *check* for allocation of TextureImage[], then, *check* for allocation of TextureImage[]->data, then free TextureImage[]->data, and then, free TextureImage[].
Right?
I hope this time I am...:D
Advertisement
yep
Thanks..a big one

This topic is closed to new replies.

Advertisement