CESNET / libyang

YANG data modeling language library
BSD 3-Clause "New" or "Revised" License
369 stars 292 forks source link

Question: lyd_print_clb from libyang1 #1860

Closed rabhat31 closed 2 years ago

rabhat31 commented 2 years ago

Hi, I have a question about the lyd_print_clb function from libyang1. https://netopeer.liberouter.org/doc/libyang/libyang1/html/group__datatree.html#ga6f340a456ffd6ed26263151c642d9f4a

Is it the duty of the caller to free the buffer (buf) in the callback function even though its declared a const void *?

michalvasko commented 2 years ago

Of course not, you must not free it.

rabhat31 commented 2 years ago

How/where does the memory pointed to by (buf) get deallocated in that case?

Say I use lyd_print_mem instead of lyd_print_clb, I get the resulting dump as a char **, which I can technically deallocate explicitly right?

michalvasko commented 2 years ago

How/where does the memory pointed to by (buf) get deallocated in that case?

Well, the printer API fully manages it, you should not worry about that.

Say I use lyd_print_mem instead of lyd_print_clb, I get the resulting dump as a char **, which I can technically deallocate explicitly right?

Yes, that is different API.

rabhat31 commented 2 years ago

Well, the printer API fully manages it, you should not worry about that.

I'd like to understand at what point this happens please. We're debugging an issue on our system where our memory size is growing quite significantly. Using valgrind and Massif, we've got some clues about where this is memory is being allocated and it always points to ly_print() through lyd_print_clb(). If I had some more clarity about how the (buf) is being deallocated, it would be really helpful to understand how to further debug this issue.

Are there any limitations to buffer size returned by lyd_print_mem() ? The writeclb in lyd_print_clb() returns a void pointer and the size of the buffer. If we only wanted a character pointer at the end of it all, could we just use these two functions interchangeably?

rabhat31 commented 2 years ago

I think this is the code block correct? case LYOUT_CALLBACK: ... ... free(msg); break; }.

michalvasko commented 2 years ago

Yes, that seems to be it, the buffer is allocated and then immediately freed so not sure what problems may that cause.