The basic use case of VIR_IDENTITY_AUTORESTORE() is in
conjunction with virIdentityElevateCurrent(). What happens is
that virIdentityElevateCurrent() gets current identity (which
increases the refcounter of thread local virIdentity object) and
returns a pointer to it. Later, when the variable goes out of
scope the virIdentityRestoreHelper() is called which calls
virIdentitySetCurrent() over the old identity. But this means
that the refcounter is increased again.
Therefore, we have to explicitly decrease the refcounter by
calling g_object_unref().
Signed-off-by: Michal Privoznik <mprivozn(a)redhat.com>
---
I've observed this imbalance whilst running qemuxml2argvtest under
valgrind:
==10412== 949 (40 direct, 909 indirect) bytes in 1 blocks are definitely lost in loss
record 524 of 539
==10412== at 0x48397EF: malloc (vg_replace_malloc.c:307)
==10412== by 0x50806F8: g_malloc (in /usr/lib64/libglib-2.0.so.0.6600.7)
==10412== by 0x50992FD: g_slice_alloc (in /usr/lib64/libglib-2.0.so.0.6600.7)
==10412== by 0x50999B9: g_slice_alloc0 (in /usr/lib64/libglib-2.0.so.0.6600.7)
==10412== by 0x518D9AB: g_type_create_instance (in
/usr/lib64/libgobject-2.0.so.0.6600.7)
==10412== by 0x51739DC: g_object_new_internal (in
/usr/lib64/libgobject-2.0.so.0.6600.7)
==10412== by 0x5174F5C: g_object_new_with_properties (in
/usr/lib64/libgobject-2.0.so.0.6600.7)
==10412== by 0x5175978: g_object_new (in /usr/lib64/libgobject-2.0.so.0.6600.7)
==10412== by 0x496C3C6: virIdentityNew (viridentity.c:407)
==10412== by 0x496BF1A: virIdentityGetSystem (viridentity.c:318)
==10412== by 0x117CEF: testCompareXMLToArgv (qemuxml2argvtest.c:653)
==10412== by 0x148505: virTestRun (testutils.c:142)
Test #315 doesn't tickle the bug, while test #316 does.
src/util/viridentity.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/src/util/viridentity.c b/src/util/viridentity.c
index e7e5c31241..eb77f69e2e 100644
--- a/src/util/viridentity.c
+++ b/src/util/viridentity.c
@@ -197,8 +197,12 @@ void virIdentityRestoreHelper(virIdentity **identptr)
{
virIdentity *ident = *identptr;
- if (ident != NULL)
+ if (ident != NULL) {
virIdentitySetCurrent(ident);
+ /* virIdentitySetCurrent() grabs its own reference.
+ * We don't need ours anymore. */
+ g_object_unref(ident);
+ }
}
#define TOKEN_BYTES 16
--
2.26.3