Advertisement

Why free(TextureImage[0]->data) created in a function

Started by January 13, 2003 08:54 PM
3 comments, last by Cat_B 22 years, 1 month ago
OK, I am not a really experienced programmer but in NeHe''s function, the TextureImage array holding the bitmap is a local variable, so isn''t it automatically destroyed once the function returns? If so, why do the free memory stuff? (I am probably missing something........) Thanx int LoadGLTextures() { int Status=FALSE; AUX_RGBImageRec *TextureImage[1]; memset(TextureImage,0,sizeof(void *)*1); if (TextureImage[0]=LoadBMP("aquatic.bmp")) { ... ..... . . . // free up memory used if (TextureImage[0]) { if (TextureImage[0]->data){ free(TextureImage[0]->data); } free(TextureImage[0]); }
The reason TextureImage[0] must be freed is because TextureImage is an array of pointers. TextureImage is a local variable and will be automatically cleaned up when it goes out of scope, but the first element of the array is allocated in the function dynamically...

memset(TextureImage,0,sizeof(void *)*1);
if (TextureImage[0]=LoadBMP("aquatic.bmp"))

TextureImage[0] = LoadBmp(...) sets the pointer. The program will not automatically free this. The array (which is actually a pointer) will be freed but the pointer within the array will not.
Advertisement
Thank you, I have been digesting what you said and dissecting the code for the last couple of hours, geesh! I think I now it (man, its like reading french....void* is a pointer; *1 for one element, etc...)

But the auxDIBImageLoad must have a malloc() function in it, right? I mean, if auxDIBImageLoad just created & returned a pointer to where the bitmap file starts and that's it, without doing a malloc() then that pointer (array element 0) would just be disposed of when it goes out of scope? I mean, there's nothing special about pointers in this way, right?

Anyway, thank you very much- you have helped me a lot!!


[edited by - Cat_B on January 13, 2003 11:48:17 PM]
yes. pretty much.

when you call LoadBMP / auxDIBImageLoad it will almost certainly allocate a block of memory to the data pointer, and set the original memory pointer you have to a newly allocated AUX_RGBImageRec structure. Hence, if you leave it, then when the function finished, the pointers are destroyed, but the memory stays allocated, so it must be freed, otherwise you get a memory leak.

It''s good practise to keep memory leaking to zero or an absolute minimum possible. (a well setup debug compile will list memory leaks after the program has terminated btw)

pointers are pretty good overall, but memory leaking is certainly a problem. I personally am shifting my code base over to a ''reference'' class, or smart pointer, or whatever you want to call it. I''d highly reccommend everyone else consider this before attempting any large scale projects. If done right it can make pointer management a thing of the past.

| - Project-X - my mega project.. big things comming soon - | - adDeath - an ad blocker I made - | - email me - |
Thanx for the tip re having the debugger list memory leaks- I will try that.

So memory is allocated whenever something is loaded into memory (duh! just realized that ). So even in opening a file, memory somewhere has been allocated for it and subsequently destroyed somewhere behind the scenes during a file close operation. And so, if I didn''t close the file, that would be a memory leak.

Thank you- its starting to make sense

This topic is closed to new replies.

Advertisement