[ldns-users] ldns memory management

Willem Toorop willem at nlnetlabs.nl
Thu Jan 17 15:39:02 UTC 2013

Hi Matthew,

Op 16-01-13 16:24, Matthew Keeler schreef:
> I have a question which I have not been able to answer by reading
> through the documentation.
> There are many methods such as lens_pkt_all, lens_rr_list_pop_rr or
> lens_rr_owner etc.. These methods return pointers to ldns structures. 
> 1) Are the pointers to copies of the data?
> 2) Should the respective _free functions be called on them once finished
> and if so will a subsequent call to the function still return data?

- ldns_pkt_all, The documentation states that a new rr_list clone
	is returned. This means that the list is newly created and must
	be freed, and every rr in the list is copied (cloned) so they
	must be freed (with all their data) as well.

	When after popping some rr's you have a piece of list left you
	wish to dispose of, you have to use ldns_rr_list_deep_free so
	that both the list and the rr's (with all their data) are freed.

	Every call to ldns_pkt_all creates a new rr_list with all rr's
	cloned from the original question, answer, authority and
	additional section.

- ldns_rr_list_pop_rr, The rr comes directly from the list and is not
	copied. You inherit the responsibility to free it from the list.
	When the list came from ldns_pkt_all, it means you inherit the
	responsibility to free the rr!

- ldns_rr_owner, A reference to the rr's owner is returned and should
	thus not be freed separately.

> In general if I make a call to ldns_pkt_all it returns a list of rrs. I
> loop over it and pop the rrs. When I am done processing the rr should I
> free it as I don't think it is associated with the list.

The rr on a ldns_pkt_all list is a clone of the original rr and should
be freed.

> If I do free
> them, what happens if I call ldns_pkt_all again in another part of the
> code? Is the actual underlying data still there?

Yes, you get a new created list with all rr's cloned.

> Thanks
> Matt Keeler


-- Willem

PS. I have to admit that in many cases (but perhaps not this one) the
details about memory responsibility are missing from the documentation.
I usually check the source in such cases to be sure. If you use doxygen
documentation (like at
http://www.nlnetlabs.nl/projects/ldns/doc/index.html ) you can
conveniently follow the link from the documentation to the source.

I do think that this information should actually be mentioned with the
return value in the documentation. I guess I need to review it some day...

More information about the ldns-users mailing list