I'm currently working on C-based software, which is a bit stuck in freeing memory in non-anti-pet mode. I am a lover of memory, lover.
My main problem is that I declare memory structures in different different areas, and these structures are passed by reference to other functions. Some of these functions may cause errors and exit ().
How can I free my structures if I go out () in the same scope, but not all my data structures are in this scope?
It seems to me that I need to wrap it all in the psuedo exception handler and handle the release handler, but it still seems ugly because it needs to know about everything that I may or may not need for free ..
View the wrapper on mallocand use them in a disciplined manner. Keep track of the allocated memory (possibly in a linked list) and use the shell to exit to list your memory to free it. You can also call memory an optional parameter and a member of your linked list structure. In applications where the allocated memory is highly dependent on size, you will find that you have a memory leak, and this can be a good way to reset the memory and analyze it.
malloc
UPDATE: Threading in your application will make this very difficult. See Other Answers to Thread Questions.
exit(). , .
, ( , ).
: . , .
, exit() , .
exit()
, , , exit() . , - , , . exit() , , 10 . , , , . main() ( main()), , .
main()
, . , , . , . , BSD, , refcnt (reference count) , "" . ( , .)
refcnt
malloc'd, /.
, malloc , - , . , exit.
, . .
- , , , .
- - , System V. , , .
, , , malloc(). , , . (APR - Apache Portable Runtime) - , , , , " C" , , , .) " ".
malloc()
, , , , . . - " , ". ( , ), , , . , , , , () , .
fopen() fclose(); , , , , . - fclose() , fopen().
fopen()
fclose()
"" ad hoc.
, , , , ( ) . , , . . , , !
int foo_create(foo_t *foo_out) { int res; foo_t foo; bar_t bar; baz_t baz; res = bar_create(&bar); if (res != 0) goto fail_bar; res = baz_create(&baz); if (res != 0) goto fail_baz; foo = malloc(sizeof(foo_s)); if (foo == NULL) goto fail_alloc; foo->bar = bar; foo->baz = baz; etc. etc. you get the idea *foo_out = foo; return 0; /* meaning OK */ /* tear down stuff */ fail_alloc: baz_destroy(baz); fail_baz: bar_destroy(bar); fail_bar: return res; /* propagate error code */ }
, , " , goto". goto, , , . , .
, , , arena.c MPS ( ).
, ... - .
greybeard, , , . , .
, - , .
, , , , (, , ).
, ( ), .
Boehm.
http://www.hpl.hp.com/personal/Hans_Boehm/gc/
, , .