xant / libhl

Simple and fast C library implementing a thread-safe API to manage hash-tables, linked lists, lock-free ring buffers and queues
GNU Lesser General Public License v3.0
420 stars 117 forks source link

about ht_delete function #4

Closed ghost closed 10 years ago

ghost commented 10 years ago

Do I need to free key/value after call the ht_delete function?

xant commented 10 years ago

keys are always copied internally (because growing the table would need to relocate them and anyway you don't want a key do be modifiable once stored in the hashtable) ... so you need to free you key (if alloc'd on the heap) when you don't need it anymore.

The value needs to be freed only if you are collecting it by passing a reference to the 'void **prev_data' argument (where to store the pointer to the value) If you pass NULL the value will be freed using the callback provided at construction time.

Note that if you didn't provide any callback the value will be leaked .... you can also just pass 'free' as callback.

In the documentation for the function (hashtable.h) I see :

change the comment if you think it's not clear enough and in case I'll be happy to merge a pull request.