angusj1 / spexamples

Automatically exported from code.google.com/p/spexamples
1 stars 0 forks source link

free the memory safely!! #8

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Chapter Darray

I use the MACRO SAFE_FREE(p) to free memory, but I want to test that 
wheather the memory was free safely or not. So when a darray was 
destroied, I still used darray_foreach() to print the darray. If you test 
it, you'll find that it still prints many integer numbers which are 
erratic. 

What is the expected output? What do you see instead?
I think when a darray is destroied, there are no numbers to print, because:
SAFE_FREE(thiz->data);
SAFE_FREE(thiz);
But in fact, many erratic numbers are printed.

Please provide any additional information below.
So I check the code. And fortunately, finally I found.
We use function free() to free memory safely
free() and malloc() must come in pairs.
The real memory area is thiz->data, so we free thiz->data and make the 
pointer thiz point to NULL. 
I think the above mentioned can avoid the problem.
Just like the following:

SAFE_FREE(thiz->data);
thix = NULL;

Original issue reported on code.google.com by gary.li....@gmail.com on 29 Apr 2010 at 6:54