[zeromq-dev] zhashx_t observation

Luca Boccassi luca.boccassi at gmail.com
Mon Oct 23 20:24:33 CEST 2017


On Mon, 2017-10-23 at 10:51 +0000, Juergen Gnoss wrote:
> 
> I use czmq from master pulled and built a few days ago.
> 
> 
> zhashx_t is meant as a hash that can handle other objects than
> strings.
> 
> That's what I understand and that's how I used it in some projects
> before.
> 
> (both, objects and keys where other than simple strings)
> 
> 
> Now I did use zframe_t as keys and (char *) as items and setup zhashx
> like so:
> 
> 
> assuming that my items (constant strings) are handled out of the box
> I just
> 
> gave comparator, duplicator and destructor to the key, which in my
> case is
> 
> another object than a string.
> 
> 
> 
> first defining the comparator
> 
> 
> ```c
> 
> static int s_scompfn(zframe_t *f1, zframe_t *f2){
>     if (zframe_eq(f1, f2)) {
>         return 0;
>     }
>     return 1;
> }
> 
> 
> ```
> 
> then
> 
> 
> ```c
> 
> zhashx_t *deviceComID_List = zhashx_new ();
> 
> 
> zhashx_set_key_destructor (deviceComID_List, (zhashx_destructor_fn *)
> zframe_destroy);
> zhashx_set_key_duplicator (deviceComID_List, (zhashx_duplicator_fn *)
> zframe_dup);
> zhashx_set_key_comparator(deviceComID_List, (zhashx_comparator_fn *)
> s_scompfn);
> 
> ```
> 
> some code to use that hash is
> 
> 
> ```c
> 
> 
>     char *item = (char *)zhashx_lookup(deviceList, cid_frame);
>     if (item == NULL) {
>         zhashx_insert(deviceList, cid_frame, "");
>     } else {
>         zhashx_delete (deviceList, cid_frame);
>     }
> 
> 
> ```
> 
> 
> Ending up that the program eats up all my memory as seen in
> 
> https://pastebin.com/JCdPGHME
> 
> (that's the valgrind massif output)
> 
> 
> 
> Is it, because my (item) strings are empty at that moment?
> 
> 
> If I give constructor and duplicator as well for the items (strings)
> to zhashx,
> 
> it works as expected.
> 
> 
> https://pastebin.com/mCvtKp6L
> 
> 
> 
> I didn't get it from Documentation, that all that duplicators and
> 
> destructors are obligatory to set.
> 
> 
> Am I the only one stumbling into that?
> 
> 
> Ju

Hi,

Yes, by default items are not destroyed as the docs for the destructor
say:

//  Set a user-defined deallocator for hash items; by default items are not
//  freed when the hash is destroyed.                                      
CZMQ_EXPORT void
    zhashx_set_destructor (zhashx_t *self, zhashx_destructor_fn destructor);

https://github.com/zeromq/czmq#zhashx---extended-generic-hash-container

-- 
Kind regards,
Luca Boccassi
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20171023/9a266950/attachment.sig>


More information about the zeromq-dev mailing list