From 9478670527b70107724dcb2cf01dc3e5d09c1803 Mon Sep 17 00:00:00 2001 From: David Vossel Date: Thu, 11 Feb 2010 18:42:25 +0000 Subject: astobj2 unit test and bug fix A bug was discovered during the creation of the astobj2 unit test. When OBJ_MULTIPLE | OBJ_UNLINK is used, the objects being returned had a ref count issue. This patch resolves that. Review: https://reviewboard.asterisk.org/r/496/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@246299 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- main/astobj2.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'main/astobj2.c') diff --git a/main/astobj2.c b/main/astobj2.c index c119a28f8..48fae0a9d 100644 --- a/main/astobj2.c +++ b/main/astobj2.c @@ -724,10 +724,14 @@ static void *internal_ao2_callback(struct ao2_container *c, AST_LIST_REMOVE_CURRENT(entry); /* update number of elements */ ast_atomic_fetchadd_int(&c->elements, -1); - /* if the object is not going to be returned, we must decrement the reference count - * to account for the reference the container was holding - */ - if (flags & OBJ_NODATA) { + + /* - When unlinking and not returning the result, (OBJ_NODATA), the ref from the container + * must be decremented. + * - When unlinking with OBJ_MULTIPLE the ref from the original container + * must be decremented regardless if OBJ_NODATA is used. This is because the result is + * returned in a new container that already holds its own ref for the object. If the ref + * from the original container is not accounted for here a memory leak occurs. */ + if (flags & (OBJ_NODATA | OBJ_MULTIPLE)) { if (tag) __ao2_ref_debug(EXTERNAL_OBJ(cur->astobj), -1, tag, file, line, funcname); else -- cgit v1.2.3