[PATCH] Allow mixing of talloc_reference and talloc_free

Sam Liddicott sam at liddicott.com
Tue Dec 16 09:11:07 GMT 2008

This patch is not complete, it is intended to serve as a talking point.

I (and others) have been frustrated in mixing talloc_reference with
talloc_free. It's currently fatal (if you are lucky) to talloc_free
something which other contexts have a talloc_reference to. It's also
awkward to have to digg out the original allocator (if it hasn't been
implicitly free'd) to call talloc_unlink.

This is one solution so that talloc_free will internally call
talloc_unlink against the original memory context, allowing the memory
to stay in-use if there are other references; but raising an error if
talloc_free is called and the original allocator has already explicitly
or implicity "released" the memory.

[This could perhaps break existing code if a talloc_reference user
"knows" that the allocating reference has gone away and then calls
talloc_free instead of talloc_unlink, but IMHO it should have been using

To consider:

1. talloc_steal support needs adding, but only on the user-called
talloc_steal, not internal uses.

2. If talloc_unlink is called directly with mem_ctx == tc->allocator
should it unset TALLOC_FLAG_UNLINK? Perhaps only if there is not more
than one reference equal to mem_ctx (also think mem_ctx == NULL).

Store the allocating mem_ctx (or stealing mem_ctx) so that
talloc_free can really do talloc_unlink; thus permitting
talloc_free to be safely called on memory that may be
referenced elsewhere.

NOT signed-off
 lib/talloc/talloc.c |   23 ++++++++++++++++++++++-
 1 files changed, 22 insertions(+), 1 deletions(-)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: b75360a1204e6a9dabd839e683bd40fcc4ac199b.diff
Type: text/x-patch
Size: 2017 bytes
Desc: not available
Url : http://lists.samba.org/archive/samba-technical/attachments/20081216/69ea76e4/b75360a1204e6a9dabd839e683bd40fcc4ac199b.bin

More information about the samba-technical mailing list